How to ensure that no non-ascii unicode characters are entered ?

Posted by Jacques René Mesrine on Stack Overflow See other posts from Stack Overflow or by Jacques René Mesrine
Published on 2010-04-26T10:28:52Z Indexed on 2010/04/26 10:33 UTC
Read the original article Hit count: 276

Filed under:
|
|

Given a java.lang.String instance, I want to verify that it doesn't contain any unicode characters that are not ASCII alphanumerics. e.g. The string should be limited to [A-Za-z0-9.]. What I'm doing now is something very inefficient:

import org.apache.commons.lang.CharUtils;

String s = ...;
char[] ch = s.toCharArray();
for( int i=0; i<ch.length; i++)
{
    if( ! CharUtils.isAsciiAlphanumeric( ch[ i ] )
        throw new InvalidInput( ch[i] + " is invalid" );
}

Is there a better way to solve this ?

© Stack Overflow or respective owner

Related posts about java

Related posts about unicode