How to ensure that no non-ascii unicode characters are entered ?
- by Jacques René Mesrine
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 ?