I have a simple textbox
in which users enter number.
Does jQuery have a isDigit
function that will allow me to show an alert box if users ente
Value validation wouldn't be a responsibility of jQuery. You can use pure JavaScript for this. Two ways that come to my mind are:
/^\d+$/.match(value)
Number(value) == value
$(document).ready(function () {
$("#cust_zip").keypress(function (e) {
//var z = document.createUserForm.cust_mobile1.value;
//alert(z);
if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
$("#errmsgzip").html("Digits Only.").show().fadeOut(3000);
return false;
}
});
});
Forget regular expressions. JavaScript has a builtin function for this: isNaN()
:
isNaN(123) // false
isNaN(-1.23) // false
isNaN(5-2) // false
isNaN(0) // false
isNaN("100") // false
isNaN("Hello") // true
isNaN("2005/12/12") // true
Just call it like so:
if (isNaN( $("#whatever").val() )) {
// It isn't a number
} else {
// It is a number
}
String.prototype.isNumeric = function() {
var s = this.replace(',', '.').replace(/\s+/g, '');
return s == 0 || (s/s);
}
usage
'9.1'.isNumeric() -> 1
'0xabc'.isNumeric() -> 1
'10,1'.isNumeric() -> 1
'str'.isNumeric() -> NaN