views:

745

answers:

2

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.

+2  A: 

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]+" }
}
Marcos Placona
accept is a native method to validate file extension. http://docs.jquery.com/Plugins/Validation/Methods/accept#extension
Boris Guéry
I über validate your answer, thanks for the quick and clean tip !!
pixelboy
Glad it helped you :-)
Marcos Placona
Shouldn't the `.` be a `^`?
Mark Byers
+1  A: 

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"

basex