Using a regular expression to validate whether input has any non digits in it
Asked Answered
R

3

6
function validInteger(theNumber){
    var anyNonDigits = new  RegExp('\D','g');
    if(parseInt(theNumber)&&!anyNonDigits.test(theNumber)){
        return true;
    }else{
        return false;
    }
}

Above is a function I've written to validate some input. I want all positive integers. The problem I'm facing is with the RegExp object. This seems like it should be super simple, but for some reason it's not working.

For example if I pass 'f5' I get true, but if I pass '5f' I get false. I'm also having problems when passing negative numbers. -3 doesn't get caught even if I stringify the variable before passing it into the RegExp. I can fix this by adding '&&parseInt(theNumber)>0' in my if statement, but I feel like the RegExp should catch that too. Thanks in advance!

Rossiter answered 7/6, 2012 at 16:52 Comment(3)
The regular expression doesn't work because you need to escape the \ character since \ also has meaning in string literal notation. new RegExp('\\D','g');Roark
...also, you wouldn't really need to g modifier, since it sounds as though even one non-digit should be enough to flag the input.Roark
@amnotiam thanks for the info on why what I was doing wasn't working. Regular expressions are something I'm still new to. Hopefully I'll change that soon.Rossiter
G
11

Simply:

function validInteger(theNumber){    
    return theNumber.match(/^\d+$/) && parseInt(theNumber) > 0;
}

Live DEMO

Or even simpler with regex only as suggested by @Eric:

return /^[0-9]\d*$/.test(theNumber);

Live DEMO

Update:

An excellent cheat sheet. The link died after 5 years, sorry.

Gherkin answered 7/6, 2012 at 16:55 Comment(9)
No need for parseInt(theNumber) > 0: /^[1-9]\d*$/Infringement
@Eric. Thanks for the suggestion, I added it with your name on it.Gherkin
i think that regex would call an empty string valid since * is "zero or more", you may want to use + instead "one or more"Pastypat
@jbabey. It's o.k. as it is because of the [0-9] before of that.Gherkin
@Gherkin oh right, the * is only applying to the \d. got it.Pastypat
Thanks for the help guys. Regular expressions have always been a problem for me, can someone point me in the right direction of a good tutorial?Rossiter
@FrankB. I added a cheat sheet, for tutorials, just google it up.Gherkin
Cheat Sheet link was so usefulMonarchist
Cheat Sheet link is broken :/Joby
A
1

If it's okay don't use RegExp, you can have:

function validInteger(theNumber){
    var number = +theNumber;

    return number > -1 && number % 1 === 0;
}

Assuming that you consider 0 as positive integer, and you don't want to make a distinction between +0 and -0.

Notice that this function will accept any value for theNumber that can be converted in a Number, so not just "string", and you can pass Number as well of course.

Aymara answered 7/6, 2012 at 17:1 Comment(3)
Probably you mean -1 < number?Roark
@amnotiam. Is there any difference (I know they do different things) between if(str.match()) and if (/.../.test(str))?Gherkin
@gdoron: Should give the same boolean coerced result. I personally prefer .test().Roark
G
0

Be simple!

function validate(num){
    return (num | 0) > 0;
};

This function will return "true" only for positive integers.

Gimpel answered 7/6, 2012 at 17:6 Comment(2)
Depends on how you want to evaluate hexadecimal notation. ('0xff' | 0) > 0; // trueRoark
("+123" | 0) > 0 => true => broken... :)Gherkin

© 2022 - 2024 — McMap. All rights reserved.