6

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.......

Vinoth13
  • 397
  • 3
  • 7
  • 16

2 Answers2

13
    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..

shanmugavel-php
  • 410
  • 1
  • 6
  • 20
2

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

Community
  • 1
  • 1
ionoy
  • 975
  • 6
  • 19