Sunday, 12 October 2014

Allow only Letter and Space using Javascript on textbox using keypress event work in all browsers?


// Allow only Alphabates and White Space for Name
function lettersOnly(e) {

    if (window.event) // IE
    {
        if (e.keyCode == 32) return true;

        if (e.KeyCode > 31 && (e.keyCode < 65 || e.keyCode > 90) && (e.keyCodee < 97 || e.keyCode > 122) || (e.keyCode >= 48 && e.keyCode <= 57) || e.keyCode == 34 || e.keyCode == 39) {
            event.returnValue = false;
            return false;
        }
        else if ((e.keyCode > 32 && e.keyCode < 48) || (e.keyCode > 57 && e.keyCode < 65) || (e.keyCode > 90 && e.keyCode < 97) || (e.keyCode >= 123 && e.keyCode <= 126)) {
            event.returnValue = false;
            return false;
        }
        else {
            return true;
        }
    }
    else {
        if (e.which == 32) return true;

        if (e.which > 31 && (e.which < 65 || e.which > 90) && (e.which < 97 || e.which > 122) || (e.which >= 48 && e.which <= 57) || e.which == 34 || e.which == 39) {
            e.preventDefault();
            return false;
        }
        else if ((e.which > 32 && e.which < 48) || (e.which > 57 && e.which < 65) || (e.which > 90 && e.which < 97) || (e.which >= 123 && e.which <= 126)) {
            event.returnValue = false;
            return false;
        }
        else {
            return true;
        }
    }
}


<asp:TextBox ID="txtName" runat="server" onkeypress="return lettersOnly(event);" />

No comments:

Post a Comment

Thanks for comments.