razor - javascript to allow only negative and positive numbers and decimal upto 6 digits on keypress -
i need validate textbox in cshtml page accept negative or positive numbers , upto 6 decimal places. have tried far.
function acceptuptosixdecimalplaceswithnegative(event, elem) { if ((event.which != 46 || $(elem).val().indexof('.') != -1) && (event.which < 48 || event.which > 57)) { if (event.keycode !== 8 && event.keycode !== 46 && event.keycode !== 9 && event.keycode !== 0 && event.keycode !== 45) { //exception event.preventdefault(); } } var text = $(elem).val(); if ((text.indexof('.') != -1) && (text.substring(text.indexof('.')).length > 6)) { if (event.keycode !== 8 && event.keycode !== 46 && event.keycode !== 9) { //exception event.preventdefault(); } }
this helping me achieve 6 digits after decimal point allows special characters , alphabets too.
any problem appreciated.
thanks.
you check value regex:
var re = /^-?\d*\.?\d{0,6}$/; var text = $(elem).val(); var isvalid = (text.match(re) !== null);
the regex means:
^ : beginning of string
-? : 1 or 0 "-"
\d* : 0 infinite numbers
\.? : 0 or 1 "."
\d{0,6} : 0 6 numbers
$ : end of string
Comments
Post a Comment