Numeric, Decimal, Alphabetic validation for the html textbox

In this article, We have explained regarding textbox validation using javascript. These are the following script...

For Numeric Validation:

function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}

How to call in the textbox :
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="return isNumberKey(this);"></asp:TextBox>

For Decimal Number Validation:

function AllowDecimalNumbersOnly(kbEvent) {
    var keyCode, keyChar;
    keyCode = kbEvent.keyCode;
    if (window.event)
        keyCode = kbEvent.keyCode;  // IE
    else
        keyCode = kbEvent.which;  //firefox         
    if (keyCode === null) return true;
    // get character
    keyChar = String.fromCharCode(keyCode);
    var charSet = "0123456789.";
    // check valid chars
    if (charSet.indexOf(keyChar) !== -1) return true;
    // control keys
    if (keyCode === null || keyCode === 0 || keyCode === 8 || keyCode === 9 || keyCode === 13 || keyCode === 27) return true;
    return false;
}

How to call in the textbox :
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="return AllowDecimalNumbersOnly(this);"></asp:TextBox>

For Alphabetical Validation:

function lettersOnly(evt) {
    evt = (evt) ? evt : event;
    var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode :
  ((evt.which) ? evt.which : 0));
    if (charCode === 32)
        return true;
    if (charCode > 31 && (charCode < 65 || charCode > 90) &&
  (charCode < 97 || charCode > 122)) {
        return false;
    }
    else
        return true;
}

How to call in the textbox :
<asp:TextBox ID="TextBox1" runat="server" OnTextChanged="return lettersOnly(this);"></asp:TextBox>

Post a Comment

0 Comments