I want to test for this input: [an optional negative sign] 2 digits [an optional . and an optional digit] like this:
-34
or -34.5333
or
Try this regex:
/^-?[0-9]{2}(?:\.[0-9]+)?$/
This below simple html code will validate the +ve & -ve numbers of 2 digits plus optional minimum of 1 digit after decimal point.
JS Code:
function validateNum() {
var patForReqdFld = /^(\-)?([\d]{2}(?:\.\d{1,})?)$/;
var value = document.getElementById('txtNum').value;
if(patForReqdFld.test(value)) {
alert('Valid Number: ' + value);
} else {
alert('Invalid Number: ' + value);
}
}
HTML Code:
<label>Enter Number: </label>
<input type="text" id="txtNum" onBlur="validateNum()"/>