简体   繁体   中英

To restrict Special Characters and allow alphabets

In the below code i want to allow alphaets and to restrict special characters.But it is allowing special characters.pls help me to solve this issue.

JS:

function AcceptAlphabetsOnly(e, t) {
    try {
        if (window.event) {
            var charCode = window.event.keyCode;
        }
        else if (e) {
            var charCode = e.which;
        }
        else { return true; }
        if ((charCode > 64 && charCode < 91) || (charCode > 96 && charCode < 123))
            return true;
        else
            return false;
    }
    catch (err) {
        alert(err.Description);
    }
};

Asp.net

<input name="data[Customer][name]" type="text" 
                             id="txtVendor" runat="server" onkeypress="return AcceptAlphabetsOnly(event,this);" />

Try the below function

function checkSpcialChar(event){
    if (!((event.keyCode >= 65) && (event.keyCode <= 90) || (event.keyCode >= 97) && (event.keyCode <= 122) || (event.keyCode >= 48) && (event.keyCode <= 57))) {
        event.returnValue = false;
        return;
    }
    event.returnValue = true;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM