Kanishka Panamaldeniya
Kanishka Panamaldeniya

Reputation: 17576

how to validate strings only for non alphabetic charactors jquery validation

hi i have a text field in my HTML form . i want to add jquery validation to add only non alphabetic characters to that field .

i want

1233$%{} is valid 

but 1233$%{}wewe is invalid

i want to return false only when alphabetic character occurs .

currently i have this but not working

 jQuery.validator.addMethod("alpha", function(value, element) {
return this.optional(element) || value != value.match(/^[a-zA-Z]+$/);
},"Only Non Alphabatic Characters Allowed.");

this sript works well if my entered string is contained with all alphabetic characters .

string "eeeeeeeeeeeeee" returns false . but "eeeeeeeeeee1111" returns true :(

please help .

i am using

http://bassistance.de/jquery-plugins/jquery-plugin-validation/

Upvotes: 1

Views: 2242

Answers (2)

coderzach
coderzach

Reputation: 388

you just want !value.match and you also don't want ^ or $ since those match the beginning and end of the line.

jQuery.validator.addMethod("alpha", function(value, element) {
  return this.optional(element) || !value.match(/[a-zA-Z]+/);
},"Only Non Alphabatic Characters Allowed.");

Upvotes: 2

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123387

try this:

 jQuery.validator.addMethod("alpha", function(value, element) {
    return (this.optional(element) || !(/[a-z]/gi).test(value));
 },"Only Non Alphabatic Characters Allowed.");

Upvotes: 1

Related Questions