Check String whether it contains only Latin characters?

前端 未结 6 1938
感动是毒
感动是毒 2021-01-01 21:36

Greetings,

I am developing GWT application where user can enter his details in Japanese. But the \'userid\' and \'password\' should only contain English characters(L

6条回答
  •  南方客
    南方客 (楼主)
    2021-01-01 22:07

    I successfully used a combination of the answers of user232624, Joachim Sauer and Tvaroh:

    static CharsetEncoder asciiEncoder = Charset.forName("US-ASCII"); // or "ISO-8859-1" for ISO Latin 1
    
    boolean isValid(String input) {    
        return Character.isLetter(ch) && asciiEncoder.canEncode(username);
    }
    

提交回复
热议问题