cancel
Showing results for 
Search instead for 
Did you mean: 

Identify Non English Character in a String

Former Member
0 Kudos

All,

We have a requirement to Identify the Non English Characters from the User Key In data and return an error message saying only valid English, Numeric and some special characters are allowed.

For Example, If the User enters data like "This is a Test data" then the return value should be true. or if he enters something like "My Native Language is inglés" then it should return false. Similarly any Chinese, russian or japansese character entryies should also return false.

How can we achieve this?

Thanks,

Nagarajan.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Nagarajan,

You could use Unicode character blocks or simply craft a regular expression that contains all the characters you need. The latter is easy to understand and gives you full control over which characters you want to allow. E.g. I assume you might want something like this:


if(!"This is a proper input string".matches("[\\s\\w\\p{Punct}]+")) {
  // Issue error message and re-get input string
}

The String method matches() takes a regular expression as input parameter. If you haven't dealt with regular expressions before, check out the Java API help for class java.util.regex.Pattern. Here's a short breakdown of the pattern I used:

<ol>

<li>The square brackets [] enclose a list of allowed characters; here you can explicitly list all allowed characters.</li>

<li>You can specify ranges like a-z as a character class, list individual characters like ;:| or utilize predefined character classes (\s for any whitespace character, \w for all letters a-z and A-Z, underscore and 0-9 and the posix class \p for a list of punctuation symbols). For a complete list check Java API help on java.util.regex.Pattern.

<li>The + at the end indicates that the characters listed can occur once or more.</li>

</ol>

There's other ways to achieve what you want, but I think this might be an easy way to start with.

Cheers, harald

Former Member
0 Kudos

Perfect and Thanks.

Answers (0)