rickyd
09-11-2008, 09:56 PM
I'm trying to write an expression to validate a natural number. It should accept any non-negative integer including '0' but not '01'. It seems like the simplest regular expression imaginable, but I can't get it to work correctly in javascript. The problem is that it will except negative numbers, and will in fact accept 'x10'.
function isNaturalNumber (str) {
var pattern = /^(0)|([1-9][0-9]*)$/;
return pattern.test(str);
}
It seems to honor the '$' but not the '^'. I know there are other ways to accomplish this, but I'd really like to understand why this doesn't work, since I need to validate some more complicated data.
function isNaturalNumber (str) {
var pattern = /^(0)|([1-9][0-9]*)$/;
return pattern.test(str);
}
It seems to honor the '$' but not the '^'. I know there are other ways to accomplish this, but I'd really like to understand why this doesn't work, since I need to validate some more complicated data.