1

I'm trying to write a custom validation function for jquery. The rule should be that the field cannot ONLY be numeric. I know how to write only numeric or only alpha but I want the rule to say that a value of "12345" would fail, but "12345A" would be validated

This is what I have for non numeric

jQuery.validator.addMethod("nonNumeric", function(value, element) {
    return this.optional(element) || !value.match(/[0-9]+/);
},"Only alphabatic characters allowed.");

but I can't figure out how to do not ONLY numeric.


Working script

Here are three rules that might be helpful, the last one is the one that answers this question.

 jQuery.validator.addMethod("noSpace", function(value, element) { 
    return value.indexOf(" ") < 0 && value != ""; 
}, "No spaces please");

jQuery.validator.addMethod("alpha", function(value, element) {

    return this.optional(element) || /^[a-z]+$/i.test(value);
},"Letters only please.");

jQuery.validator.addMethod("nonNumeric", function(value, element) {
    return this.optional(element) || isNaN(Number(value));
},"String cannot be numeric");
2
  • If I understand correctly, you want that the value contains at least on numerical and one alphabetical character? Commented Apr 12, 2012 at 22:57
  • No basically its for a username field, but I don't want users to type in an integer to try to mimic a userid. So it needs atleast one alphabetical char, it just can't be all numeric. Commented Apr 12, 2012 at 23:04

4 Answers 4

2

Use parseInt (which returns NaN if the operand isn't strictly a number) like this:

jQuery.validator.addMethod("nonNumeric", function(value, element) {
    return this.optional(element) || !isNaN(parseInt(value));
},"Only alphabatic characters allowed.");

Don't use != in this case or you could wind up with this unpleasant situation.


According to Ken Browning's comment, parseInt might not be appropriate here. Try this instead:

jQuery.validator.addMethod("nonNumeric", function(value, element) {
    return this.optional(element) || !isNaN(Number(value));
},"Only alphabatic characters allowed.");
Sign up to request clarification or add additional context in comments.

9 Comments

parseInt probably isn't appropriate here: ​alert(!!​parseInt('3x'))​​​​​​​
Mm, that's true, I'll edit with another option. I'm not 100% positive but Number should do the trick. Can't pull the double not trick here. :p
I think the OP wants the input to be valid if at least one character is alphabetical.., but I'm not sure. Btw, you cannot use the comparison operator for testing against NaN, use isNaN().
@FelixKling: Yep, that's what this'll do, unless I somehow reversed my logic and it's only returning if input is invalid, because if Number(value) equals NaN that means there's a letter in there somewhere.
Ah right... now I understand...I would probably have used something like /[a-z]/.test(value). Or use !isNaN(+value).
|
1

You can use

/[a-z]/i.test(value)

which returns true if value contains any letter between a and z.

The i modifier makes the test case insensitive.

For more information about .test(), have a look at the MDN documentation.

Comments

1

change your regular expression to this:

(/.*[a-zA-Z]+.*/)

on your code:

EDIT 1:

jQuery.validator.addMethod("nonNumeric", function(value, element) {
    return this.optional(element) || value.match(/.*[a-zA-Z]+.*/);
},"Only alphabatic characters allowed.");

Comments

1

I think this regex should be enough

/[a-z]/i.test(value);

Usage

jQuery.validator.addMethod("nonNumeric", function(value, element) {
    return this.optional(element) || !(/[a-zA-Z]/i.test(value));
},"Only alphabatic characters allowed.");

2 Comments

You don't need A-Z if you are using the i modifier... that's the whole point of it ;)
This doesn't help for non ascii characters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.