Validate Numeric value with negative and decimals using Javascript
How to validate Numeric value with negative and decimals
using Javascript
The below script shows how to validate a value to be Numeric or decimal with negative value. It will not allow the user to enter any other special characters.
Allows negative and non negative decimal values.
//Validate Numeric value (Allows negative value)
//-------------------------------------------------
function allowNegativeNumber(e) {
var charCode = (e.which) ? e.which : event.keyCode
if (charCode > 31 && (charCode < 45 || charCode > 57) || charCode == 46) {
return false;
}
return true;
}
//Validate Numeric value with negative and decimals
//-------------------------------------------------
function allowNegativeDecimalNumber(e) {
var charCode = (e.which) ? e.which : event.keyCode
if (charCode > 31 && (charCode < 45 || charCode > 57)) {
return false;
}
return true;
}
This JavaScript function you can all in onkeypress event of the textbox and it will validate the user entered value in client side.