Java Script regex for detecting non-ascii characters

How can we use a java script to restrict the use of non-ascii characters in a specific text field.? thanks in advance...

+5
source share
2 answers

Ascii is defined as characters in the range 000-177 (octal), therefore

function containsAllAscii(str) {
    return  /^[\000-\177]*$/.test(str) ;
}

http://jsfiddle.net/V5e4B/1/

You probably don't want to accept non-printable characters \000-\037, maybe your regular expression should be/\040-\0176/

+16
source

, , URL- CMS. CMS , -ascii URL-. ( ):

function verify_url(txt){
    var str=txt.replace(/^\s*|\s*$/g,""); // remove spaces
    if (str == '') {
        alert("Please enter a URL for this page.");
        document.Form1.url.focus();
        return false;
    }
    found=/^[a-zA-Z0-9._\-]*$/.test(str); // we check for specific characters. If any character does not match these allowed characters, the expression evaluates to false
    if(!found) {
        alert("The can only contain letters a thru z, A thru Z, 0 to 9, the dot, the dash and the underscore. No spaces, German specific characters or Chinese characters are allowed. Please remove all punctuation (except for the dot, if you use it), and convert all non complying characters. In German, you may convert umlaut 'o' to 'oe', or in Chinese, you may use the 'pinyin' version of the Chinese characters.");
        document.Form1.url.focus();
    }
    return found;
}
+1

All Articles