Search code examples
javascriptjqueryspecial-characters

How to allow Å Ä Ö & å ä ö inside the textbox?


I want allow user to type only alphabet ,space and Å Ä Ö & å ä ö the given character in Text Box. alphabet validation and space is working fine but what I have to do for this character Å Ä Ö & å ä ö

Now I am using below javascript function,

 function ValidateAlpha(evt) {
debugger;
evt = (evt) ? evt : event;
var charCode = (evt.charCode) ? evt.charCode : ((evt.keyCode) ? evt.keyCode : ((evt.which) ? evt.which : 0));
if (charCode != 13 && charCode > 31 && (charCode < 37 || charCode > 40) && (charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 122) && (charCode == 196 || charCode == 142))
{
swal("Enter Only Character");
return false;
}
return true;
}


Solution

  • If you want to exclude or allow specific characters, you would have to explicitly state which ones.

    You can use this web page to see which character code belongs to each character.

    As you can see in this page, the characters you mention are between 128 and 165.

    So to allow these characters, just add:

    ((charCode > 128) && (charCode < 165))
    

    Recommendation:

    To exclude all non-English characters, you can replace your whole condition with this simple one:

    if (charCode > 126)
    {
        swal("Enter Only Character");
        return false;
    }
    

    To exclude all non-English characters and allow Å Ä Ö å ä ö:

    if ((charCode >  128) && 
        (charCode != 134) && 
        (charCode != 142) && 
        (charCode != 143) && 
        (charCode != 153) && 
        (charCode != 199) && 
        (charCode != 198))
    {
        swal("Enter Only Character");
        return false;
    }