views:

307

answers:

3

I'd like to use jQuery's validation plugin to validate a field that only accepts alphabetical characters, but there doesn't seem to be a defined rule for it. I've searched google but I've found nothing useful.

Any ideas?

Appreciate your help.

+2  A: 

If you include the additional methods file, here's the current file for 1.7: http://ajax.microsoft.com/ajax/jquery.validate/1.7/additional-methods.js

You can use the lettersonly rule :) The additional methods are part of the zip you download, you can always find the latest here.

Here's an example:

$("form").validate({
  rules: {
    myField: { lettersonly: true }
  }
});

It's worth noting, each additional method is independent, you can include that specific one, just place this before your .validate() call:

jQuery.validator.addMethod("lettersonly", function(value, element) {
  return this.optional(element) || /^[a-z]+$/i.test(value);
}, "Letters only please"); 
Nick Craver
Thanks! works great.
KeyStroke
A: 

I think you can use the excellent jquery ketchup plugin for that.

Sarfraz
A: 

You may want to try a regex, like in this question. Check out the first answer (the accepted one). Then you only need to define a letters-only regex ( [A-Za-z] or something)

nc3b