Regex for a number which can starts with 0 - javascript

I use this regex for my field:
/^([1-9]([0-9]){0,3}?)((\.|\,)\d{1,2})?$/;
What I want to do is to allow the user to enter 0 as a beginning of the number, but in this cas, he must enter at least a second digit diffrent from 0 and the same rule is applied for the third and fourth digits.
Example:
01 -> valid
00 -> not valid
0 -> not valid
1 -> valid
In short, zero value must not be allowed. How can I do this using Regex? or would it be better if I just do it with a javascript script?

If you want to only match numbers that have 1 to 4 digits in the part before a decimal separator, and up to 2 digits after the decimal separator (I deduce it from the regex you used) and that do not start with 00 (that requirement comes from your verbal explanation), use
/^(?!00)(?!0+(?:[.,]0+)?$)\d{1,~4}(?:[.,]\d{1,2})?$/
See the regex demo.
Details
^- start of string
(?!00) - no two 0s at the start of the string
(?!0+(?:[.,]0+)?$) - a negative lookahead that fails the match if there are one or more 0s, followed with an optional sequence of . or , followed with one or more zeros up to the string end
\d{1,4} - any 1 to 4 digits
(?:[.,]\d{1,2})? - 1 or 0 occurrences of
[.,] - a . or ,
\d{1,2} - any 1 or 2 digits
$ - end of string.
JS demo:
var ss = ['1','1.1','01','01.3','023.45','0','00','0.0','0.00','0001'];
var rx = /^(?!00)(?!0+(?:[.,]0+)?$)\d{1,4}(?:[.,]\d{1,2})?$/;
for (var s of ss) {
var result = rx.test(s);
console.log(s, "=>", result);
}

Try combining JS and RegEx:
if (parseInt(value) != 0 && value.test(/\d+([,.]\d{1,2})?/)) {
//valid
}

Related

regular expression no more than one digit

how to make a regular expression into one number (only numbers) and that it does not exceed 10 from 0 to 10
/^[1-9][1]*$/.test(message)
It doesn't work that way for me.
To specify the amount of a specific character use {} instead of [], in this case, as it is only one digit, you do not need to specify a count as 1 is default:
/^[0-9]$/.test(message)
I assume you mean you want to match a single digit between 0 and 10. If not please comment to clarify.
Hope this helps.
Assuming you only want integers or whole numbers, then use:
/^(?:[0-9]|10)$/
If you want to allow for decimals, then use:
/^(?:[0-9](?:\.\d+)?|10(?:\.0+)?)$/
The second regex says to match:
^ from the start of the number
(?:
[0-9] 0 to 9
(?:\.\d+)? any optional decimal component
| OR
10 match integer 10
(?:\.0+)? optional zero decimal only
)
$ end of the number

regex for age with 2 decimal points

What is regex for age with 2 decimal points value with max value 99.11
Valid examples:
1
12.01
16.06
20.11
Invalid examples:
.0
1.12 (decimal value should be <12)
1.13 (decimal value should be <12)
12.111
100.00 (2 digit before decimal point, max 99.11)
I have tried ^[0-9]\d{0,1}(\.\d[0-1]\d{0,1})*(,\d+)?$ regex but it is not following all criteria.
can anyone help me?
You can try with this pattern:
/^\d{1,2}(\.(0[0-9]|1[01]))?$/
var pattern = /^\d{1,2}(\.(0[0-9]|1[01]))?$/;
console.log(pattern.test('1'));
console.log(pattern.test('12.01'));
console.log(pattern.test('16.06'));
console.log(pattern.test('20.11'));
console.log(pattern.test('.0'));
console.log(pattern.test('1.12'));
console.log(pattern.test('1.13'));
console.log(pattern.test('12.111'));
console.log(pattern.test('100.00'));
Try this.
/^\d\d?(\.(0\d|1[01]))?$/
Description:
^\d\d? - from the start of the string, match 1 or 2 numbers
After that/those numbers, optionally match . plus 2 numbers
The two numbers must be 0 and any number, else 1 and 0 or 1
Anchor to the end of the string
You are missing the two different patterns of floating part, which must be handled with a |.
A character-optimal solution is ^\d{1,2}(\.[0]\d|\.1[01])?$.

Allow integers from 0 to 59 after ':' in a string [duplicate]

I am trying to validate minutes:seconds input where minutes can be 07 or 7.
I can get 07:35 validated using below but not 7:35. When I process the input I can append a zero if values is less than 9 but want to be able to let users type 7:35 as well.
^([0-5]\d:[0-5]\d$)
You may make the first munite digit optional:
^[0-5]?\d:[0-5]\d$
^
See the regex demo
Details
^ - start of string
[0-5]? - an optional (1 or 0 repetitions) of a 0 to 5 digit
\d - any 1 digit
: - a : char
[0-5] - a digit from 0 to 5
\d - any 1 digit
$ - end of string.

Javascript regular expression if no digit specified before the period (.)

I have tried following expression which works fine to allow only 2 decimal places:
/^[0-9]+(\.[0-9]{1,2})?$/
But it's not considering the value like ".34" or ".5" as there isn't any digit before the period.
How can I update my expression so that, it should take digit "0" before period if nothing is specified before period.
so .34 => 0.34 with valid 2 decimal places expression.
You can use this regex:
/^\d*\.?\d{1,2}$/
RegEx Demo
If you want to format decimal numbers up to 2 decimal points then use toFixed:
var n = ".34";
var str = parseFloat(n).toFixed(2) // will do rounding as well
//=> 0.34
Use * instead of +
^[0-9]*(\.[0-9]{1,2})?$
+: Matches 1 or more of the preceding token.
*: Matches 0 or more of the preceding token.

Regex to validate decimals

I need to validate the integers with the below pattern. Integer part can have up to 5 characters and the fraction value can have up to 2 chars. (fraction value is optional). Leading + or - is also optional.
(+/-) mmmmm (5).nn (2)
Test data
Valid
-1
+1
1
+.1
-.1
.1
+11111.11
-11111.11
11111
Invalid
+111111.11
-111111.11
+11111.111
-11111.111
11111.111
111111.11
+1.
-1.
1.
This is what I currently use
[+-]?\d{0,5}\.?\d{1,2}?
Is this correct? Am I missing something obvious?
Here is the test case.
EDIT
If there is an additional constraint to have the number of digits from the scale is included in the precision.
For example DECIMAL(5, 2) defines numbers of the form 1234.5 whereas DECIMAL(5, 5) defines numbers of the form 1.2345. How should I change this?
Do:
^[+-]?(?:[0-9]{1,5})?(?:\.[0-9]{1,2})?$
^[+-]? matches + or - at start, optional
(?:[0-9]{1,5})? matches one to five digits, optional
(?:\.[0-9]{1,2})?$ matches a literal dot, followed by one or two digits at the end, optional. As the literal . is inside the non-captured group with the digits pattern following, it will only be matched when there are required digits afterwards
Demo
In Javascript, you can validate a number using
Number(n) === Number(n)
For any non-number values of n, like "abc" or [1, 2, 3], Number(n) will return NaN. Given that NaN !== NaN in any case, you can easily tell if n is a number.
Number constructor works fine with almost any form of number representation:
Number(123) // -> 123
Number('123') // -> 123
Number('+12e+3') // -> 12000
Number('-1.2e-3') // -> -0.0012
Number('--123') // -> NaN
However, if you are constrained to using regular expressions, it's very close to what you described:
Optional "+" or "-".
0 to 5 digits.
Optional "." and then 1 to 2 digits in decimal part. The whole part is optional, meaning that a single dot with no digits is invalid
And no exponential form.
That would be
/^[+-]?\d{0,5}(\.\d{1,2})?$/
so that
/^[+-]?\d{0,5}(\.\d{1,2})?$/.test(123) // -> true
/^[+-]?\d{0,5}(\.\d{1,2})?$/.test('123') // -> true
/^[+-]?\d{0,5}(\.\d{1,2})?$/.test('+123.45') // -> true
/^[+-]?\d{0,5}(\.\d{1,2})?$/.test('--123') // -> false
/^[+-]?\d{0,5}(\.\d{1,2})?$/.test('-123.456') // -> false
Please note that the part (\.\d{1,2})? is whole optional. In your case, either dot or two decimal digits are optional, so "123." would be a valid number. In Javascript, it is valid, though, so there shouldn't be any problem with that.
A slight modification in your regex works :
^[+-]?[0-9]{0,5}?(\.[0-9]{1,2})?$
or
^[+-]?\d{0,5}?(\.\d{1,2})?$
Demo1
Demo2

Categories

Resources