开发者

Jquery validation for alphabets

开发者 https://www.devze.com 2023-02-13 23:04 出处:网络
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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消