I have downloaded jquery validation.js and using it. I need to validate for alphabets in that. For this what i need to do in validation.js
My js is like this,
categoryname: {
required: true,
minlength: 2
},
messages: {
categoryname: "Enter the category name",
the above code ask for required field and if field is empty it will show the below message. here i need to validate for only alphabets too.......
jQuery.validator.addMethod("alphanumericspecial", function(value, element) {
return this.optional(element) || value == value.match(/^[-a-zA-Z0-9_ ]+$/);
}, "Only letters, Numbers & Space/underscore Allowed.");
jQuery.validator.addMethod("alpha", function(value, element) {
return this.optional(element) || value == value.match(/^[a-zA-Z]+$/);
},"Only Characters Allowed.");
jQuery.validator.addMethod("alphanumeric", function(value, element) {
return this.optional(element) || value == value.match(/^[a-z0-9A-Z#]+$/);
},"Only Characters, Numbers & Hash Allowed.");
U can create functions easily like this bro..
You will need to add extra method to validation library, like that:
$.validator.addMethod("alpha", function(value,element)
{
return this.optional(element) || /^[a-zA-Z]$/i.test(value);
}, "Alphabets only");
Then you can add it to your validation rules.
Otherwise, you can define generic "regexp" rule, as described in this answer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With