Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery validate plugin : accept letters only?

Tags:

I'm using the validate plugin from http://bassistance.de/jquery-plugins/jquery-plugin-validation/

What i'm trying to find is a way to make some of my form fields accept letters only, no numbers, special chars etc...

Any idea people ? Thanks a lot.

like image 489
pixelboy Avatar asked Mar 19 '10 10:03

pixelboy


People also ask

What is JQuery validate?

Validation in JQuery: Using JQuery, a form is validated on the client-side before it is submitted to the server, hence saves the time and reduce the load on the server. Form Validation means to validate or check whether all the values are filled correctly or not.


2 Answers

Simply add a custom validator, and use it like this:

jQuery.validator.addMethod("accept", function(value, element, param) {
  return value.match(new RegExp("." + param + "$"));
});

Only numbers:

rules: {
  field: { accept: "[0-9]+" }
}

Only letters

rules: {
  field: { accept: "[a-zA-Z]+" }
}
like image 190
Marcos Placona Avatar answered Dec 17 '22 03:12

Marcos Placona


A small change.

jQuery.validator.addMethod("accept", function(value, element, param) {
    return value.match(new RegExp("^" + param + "$"));
});

Because that way it was accepting expressions like "#abc".

like image 45
basex Avatar answered Dec 17 '22 02:12

basex