If you are using validation.js to add validation in your page and want to add some custom validation on the same, like I want a user can only enter alphabetic character with comma, dashes and space on first name text box. Please follow below code:
<script src="YOUR_VALIDATION_FILE_PATH/js/validation.js"></script>
$.validator.addMethod("loginRegex", function(value, element) {
return this.optional(element) || /^[a-z\s\`\'\-]+$/i.test(value);
}, "Username must contain only letters, spaces ` 'or dashes.");
jQuery("#registration-frm").validate({
// Specify the validation rules
rules : {
user_fname : {
required : true,
loginRegex: true, // You can see we have added our custom function here
maxlength : 30
}
},
messages : {
user_fname : {
required : "Please enter your first name",
maxlength : "Only maximum {0} characters are required"
}
},
submitHandler : function(form) {
form.submit();
}
});
0 Comment(s)