Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Script regular expression for detecting non-ascii characters

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

like image 610
sasidhar Avatar asked Mar 03 '11 19:03

sasidhar


People also ask

How can you tell if a character is non ASCII?

Return value The isascii() function returns a boolean value where True indicates that the string contains all ASCII characters and False indicates that the string contains some non-ASCII characters.

What does \+ mean in regex?

To match a character having special meaning in regex, you need to use a escape sequence prefix with a backslash ( \ ). E.g., \. matches "." ; regex \+ matches "+" ; and regex \( matches "(" .

Does regex work with Unicode?

RegexBuddy's regex engine is fully Unicode-based starting with version 2.0. 0.

How do you remove non ascii characters?

Using. Bring out the command palette with CTRL+SHIFT+P (Windows, Linux) or CMD+SHIFT+P on Mac. Type Remove Non Ascii Chars until you see the commands. Select Remove non Ascii characters (File) for removing in the entire file, or Remove non Ascii characters (Select) for removing only in the selected text.


1 Answers

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

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

console.log ( containsAllAscii('Hello123-1`11'));
console.log ( containsAllAscii('ábcdé'));

You probably don't want to accept non-printing characters \000-\037, maybe your regex should be /\040-\176/

like image 187
Juan Mendes Avatar answered Nov 15 '22 06:11

Juan Mendes