140
rated 0 times
[
143]
[
3]
/ answers: 1 / hits: 56886
/ 11 Years ago, sat, january 18, 2014, 12:00:00
I want to write a regular expression in which allows
- backspace
- 0-9 digits
- optional fractional part with two decimals (no limit on integral part how many digits there can be)
For example:
- Allowed lists is [12 , 232.0 , 23.(with only dot) , 345.09 , 78.23 , 134.00 , 0.21 , .21 , .02 , .01 .12 ]
- Not allowed are [12.878 , 34.343.334 , .0003 ]
The use of this regular expression would be like on javascript event
<input type=text onKeyPress=validatenumber(event); /><br>
My code is
function validatenumber(evt) {
var theEvent = evt || window.event;
var key = theEvent.keyCode || theEvent.which;
key = String.fromCharCode( key );
var regex = /^[0-9b]+$/; // allow only numbers [0-9]
if( !regex.test(key) ) {
theEvent.returnValue = false;
if(theEvent.preventDefault) theEvent.preventDefault();
}
}
I want to change only this line with the new regex:
var regex = /^[0-9b]+$/; // allow only numbers [0-9]
More From » jquery