Jquery validation plugin how to add multiple custom posts to custom method

I am using jquery validation plugin

I added a custom method using addmethod , which in turn calls another UK telephone number validation method

here is my code (simplified):

HTML

 <form id="myform"> <label for="field">Required, telephone: </label> <input class="required" id="field" name="field" /> <br/> <input type="submit" value="Submit" /> </form> 

JQuery

 $(document).ready(function(){ $("#myform").validate({ rules:{ field:{ required:true; UKTelNumber:true } } }); }); jQuery.validator.addMethod("UKTelNumber", function(value,element) { if (!checkUKTelephone (value)) { alert (telNumberErrors[telNumberErrorNo]); return false; } else { return true } },jQuery.validator.format(telNumberErrors[telNumberErrorNo])); 

The checkUKTelephone function sets the var telNumberErrorNo value according to the type of error. All error messages are present in the telNumberErrors array.

My requirement now is how to display the error messages that are now being warned.

passing jQuery.validator.format(telNumberErrors[telNumberErrorNo]) as a message (third option) addmethod does not help.

I also tried passing only this telNumberErrors[telNumberErrorNo] , but it only showed one message every time the message contained in telNumberErrors[0]

plz help me

Thank you in advance

+4
source share
2 answers

Well, I got a solution to my question, so I decided to answer my question that it can help others

just create a function and return a warning error

 var dispError = function(){ return telNumberErrors[telNumberErrorNo]; } 

and then pass this function as the third argument to addMethod

 jQuery.validator.addMethod("UKTelNumber", function(value,element) { if (!checkUKTelephone (value)) { return false; } else { return true } },dispError); 

then call the validation method

 $(document).ready(function(){ $("#myform").validate({ rules:{ field:{ required:true; UKTelNumber:true } } }); }); 
+6
source

Here is my example:

 $.validator.addMethod('variable', function (value, element, params) { if (!/[az]/i.test(value[0])) { return this.optional(element) || false; } else if (/[~`!#$%\^&*+=\-\[\]\\';.,/(){}|\\":<>\?]/g.test(value)) { return this.optional(element) || false; } else { return this.optional(element) || true; } }, function(error, element) { var value = $(element).val(); if (!/[az]/i.test(value[0])) { return 'First character must be a letter' } if (/[~`!#$%\^&*+=\-\[\]\\';.,/(){}|\\":<>\?]/g.test(value)) { return 'No special characters except: _' } }) 
+1
source

All Articles