Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery validation for alphabets

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

like image 727
Vinoth13 Avatar asked Mar 01 '11 14:03

Vinoth13


2 Answers

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

like image 177
shanmugavel-php Avatar answered Nov 10 '22 03:11

shanmugavel-php


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

like image 33
ionoy Avatar answered Nov 10 '22 03:11

ionoy