I've been trying to validate through a regular expression that a number has certain integers and decimal numbers but I can't get it to validate it.
I want it to only be able to have at most 3 integers and at most 5 decimals.
I have this:
var numero = 22.222222;
var exp = /^[0-9]{3}+(,[0-9]{5}+)?$/;
if(exp.test(numero)){
console.log("correcto");
}else{
console.log("error");
}
That number should give me an error because it exceeds 5 decimal places.
To indicate "at most X characters" you must say
{1,X}
.Therefore, the expression must be of the type:
Veil in action:
Also notice that your quantifiers
+
didn't do any function, so I removed them.To make something optional you must add an
?
end to it after the block.In your case: