Reputation: 270
Validations i'm trying :
Edit :
var regex = '(?!0)\d+(?:\.\d+)?$';
function getValue() {
// passing value 0009.99 and 0009.00 and 100
return document.getElementById("myinput").value;
}
function test() {
alert(regex.test(getValue()));
}
function match() {
alert(getValue().match(regex));
}
Upvotes: 1
Views: 98
Reputation: 10929
Your first and second seems to Work just fine, the third can be achieved with the following regex:
/(?!0)\d+\.\d+$/
It starts by looking forward for zeros (skipping them), then it matches any number of digits followed by a dot and more digits. If you want the digits to be optional you can replace the plus '+
' with a star '*
'.
Edit:
If you want to allow integers, you can use this regex:
/(?!0)\d+(?:\.\d+)?$/
That makes the dot and the digits after that optional.
BTW: Your jsfiddle does not help in answering.
Edit2:
To create a Regex using quotes you must use the following syntax:
var regex = new RegExp('(?!0)\d+(?:\.\d+)?$');
Edit3:
I forgot to mention, you need to double escape backslashes, it should be:
var regex = new RegExp('(?!0)\\d+(?:\\.\\d+)?$');
Now it should Work directly in your code.
Upvotes: 4