JQuery validate plugin - how to NOT show labels "this field is required"

How to disable the display "this field is required" for ALL of my text input fields? I would prefer not to do something like this:

$("myForm").validate({ messages: { myField1: { required: '' }, myField2: { required: '' }, myField3: { required: '' }} }); 
+4
source share
5 answers

You can simply set the default message for an empty string.

 $.validator.messages.required = ''; 
+8
source

There are several ways:

  • Use errorPlacement callback to forward default labeling with your own function.
  • Use the errorLabelContainer parameter to place error elements in one container and hide the container.
  • set the default messages for the validator to an empty string - see docs setDefaults
  • use css to hide label.error
+3
source

You can assign a class to each of the elements, and then loop over that class and cross-check their type as input

 $(".frmPrc").each(function(index) { if ($(this).attr("type")=="checkbox") { if ($(this).is(":checked")) { arrFrmData.push({ "ID":index, "Field":$(this).attr("id"), "Value":1, "Title":$(this).attr("title")}); } else { arrFrmData.push({ "ID":index, "Field":$(this).attr("id"), "Value":0, "Title":$(this).attr("title")}); } } else if ($(this).attr("type")=="radio") { if ($(this).is(":checked")) { arrFrmData.push({ "ID":index, "Field":$(this).attr("name"), "Value":$(this).val(), "Title":$(this).attr("title")}); } } else { arrFrmData.push({ "ID":index, "Field":$(this).attr("id"), "Value":$(this).val(), "Title":$(this).attr("title")}); } }); 

it's just a quick copy and paste from an old script ... as you can see, it can be shortened ... but it gives the basic idea of โ€‹โ€‹using "required" fields ...

0
source

I used to add a class to all input elements instead of using Validate Plugins, which should check:

 <input type="text" class=toValidate/> 

....

  function validateForm(){ $(".toValidate").each(function (){ if($(this).val()==""){ do something } }) } 
0
source

Only this and your problem will be solved!

 var validator = $("#MyForm").validate({errorPlacement: function() { return true; }}); 
-1
source

Source: https://habr.com/ru/post/1416245/


All Articles