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");
Brian
  • 4,328
  • 13
  • 58
  • 103
  • If I understand correctly, you want that the value contains at least on numerical and one alphabetical character? – Felix Kling Apr 12 '12 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. – Brian Apr 12 '12 at 23:04

4 Answers4

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.");
Community
  • 1
  • 1
Elliot Bonneville
  • 51,872
  • 23
  • 96
  • 123
  • parseInt probably isn't appropriate here: ​alert(!!​parseInt('3x'))​​​​​​​ – Ken Browning Apr 12 '12 at 22:46
  • 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 – Elliot Bonneville Apr 12 '12 at 22:48
  • 1
    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()`. – Felix Kling Apr 12 '12 at 22:56
  • @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. – Elliot Bonneville Apr 12 '12 at 22:58
  • Ah right... now I understand...I would probably have used something like `/[a-z]/.test(value)`. Or use `!isNaN(+value)`. – Felix Kling Apr 12 '12 at 22:59
  • @FelixKling: Nice, nice nice. I'm no JS expert (yet) so this is the best I've got. =) – Elliot Bonneville Apr 12 '12 at 23:00
  • I corrected your code... even if `Number('123foo')` returns `NaN`, `Number('123foo') != NaN` is `false`, simply because `NaN` is not equal to any value, not even to itself. – Felix Kling Apr 12 '12 at 23:03
  • @FelixKling: Oops, that's right... and I just read that somewhere, too. Thanks! – Elliot Bonneville Apr 12 '12 at 23:04
  • The edit was mostly correct except for the not ! was reversed as mentioned. I'll add the custom rules up top incase people come across this – Brian Apr 13 '12 at 00:52
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.");
pylover
  • 7,670
  • 8
  • 51
  • 73
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.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
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.");
Starx
  • 77,474
  • 47
  • 185
  • 261