Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery validation plugin: accept only alphabetical characters?

Tags:

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.

like image 819
KeyStroke Avatar asked May 08 '10 13:05

KeyStroke


People also ask

How to validate only alphabets in JQuery?

addMethod("lettersonly", function(value, element) { return this. optional(element) || /^[a-z," "]+$/i. test(value); }, "Letters and spaces only please"); [a-z, " "] by adding the comma and quotation marks, you are allowing spaces, commas and quotation marks into the input box.

Is JQuery alphanumeric?

Alphanum is a small yet useful JQuery plugin that allows you to restrict a text field to only accept specify characters (alphabetic, numeric or alphanumeric characters).

What is JQuery validation?

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.


1 Answers

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");  
like image 99
Nick Craver Avatar answered Sep 18 '22 17:09

Nick Craver