Reputation: 5018
Hi I have a javascript function that checks for signed integer with 0 to 12 length, i also want to see if there are any leading 0's like 0012 should return false.
function sInteger0to12(str) {
str = str.replace(/^\s+|\s+$/g, '');
return /^[-+]?\d{0,12}$/.test(str);
}
any help will be appreciated.
Upvotes: 1
Views: 2674
Reputation: 10998
You need to cover three cases
these cases equate to
which adds up to
^()|(0)|([+-]?[1-9]\d{0,11})$
Upvotes: 0
Reputation: 838916
I'm assuming that the following should match:
1
+1
-1
0
-123456789012
<empty>
And these should fail:
-
+
01
-01
1234567890123
00
+0
-0
If you disagree with my decisions above, please let me know and I will try to fix the regex.
Here's a regex you can use:
/^([-+]?[1-9]\d{,11}|0)?$/
Upvotes: 6
Reputation: 887887
Like this:
/^[-+]?[1-9]\d{0,11}$/
You'll need to check for '0'
separately.
Upvotes: 1