All basic validation by using a class:
$('.IsInteger,.IsDecimal').focus(function (e) {
if (this.value == "0") {
this.value = "";
}
});
$('.IsInteger,.IsDecimal').blur(function (e) {
if (this.value == "") {
this.value = "0";
}
});
$('.IsInteger').keypress(function (e) {
var charCode = (e.which) ? e.which : e.keyCode;
if (charCode > 31
&& (charCode < 48 || charCode > 57))
return false;
});
$('.IsDecimal').keypress(function (e) {
var charCode = (e.which) ? e.which : e.keyCode;
if (this.value.indexOf(".") > 0) {
if (charCode == 46) {
return false;
}
}
if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
return false;
});
$('.IsSpecialChar').keypress(function (e) {
if (e.keyCode != 8 && e.keyCode != 46 && e.keyCode != 37 && e.keyCode != 38 && e.keyCode != 39 && e.keyCode != 40)
return false;
else
return true;
});
$('.IsMaxLength').keypress(function (e) {
var length = $(this).attr("maxlength");
return (this.value.length <= length);
});
$('.IsPhoneNumber').keyup(function (e) {
var numbers = this.value.replace(/\D/g, ''),
char = { 0: '(', 3: ') ', 6: ' - ' };
this.value = '';
for (var i = 0; i < numbers.length; i++) {
this.value += (char[i] || '') + numbers[i];
}
});
$('.IsEmail').blur(function (e) {
var flag = false;
var email = this.value;
if (email.length > 0) {
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/;
flag = regex.test(email);
}
if (!flag)
this.value = "";
});
Example:
<asp:TextBox
runat="server"
ID="txtDeliveryFee"
TextMode="SingleLine"
CssClass="form-control IsInteger"
MaxLength="3"
Text="0"
></asp:TextBox>
Just put the class name in the input.