简体   繁体   中英

How can I check if the user input is a word and not a special character like (*!?)?

I have this code(Option1):

if(!firstName.getText().toString().matches("/w+/")){
            error = error + getString(R.string.error_first_name_incorrect_resgister_activity) +"\n";

And I have this code(Option2) that doesnt do what is expected(allow the user to only insert those characters)

if(!lastName.getText().toString().matches("/^[a-zA-ZàáâäãåąčćęèéêëėįìíîïłńòóôöõøùúûüųūÿýżźñçčšžÀÁÂÄÃÅĄĆČĖĘÈÉÊËÌÍÎÏĮŁŃÒÓÔÖÕØÙÚÛÜŲŪŸÝŻŹÑßÇŒÆČŠŽ∂ð ,.\'-]+$/u")){
            error = error + getString(R.string.error_last_name_incorrect_resgister_activity) +"\n";

You can use pattern (?ui)\\w+ to match with unicode symbols

Java Pattern

        Pattern p = Pattern.compile("[^a-z0-9 ]", Pattern.CASE_INSENSITIVE);
        Matcher m = p.matcher(your_text_string);
        boolean b = m.find();

        if (b) {
            // error occurred special chars
        }

First as Null suggested you could just check the string length. Like this:

if(firstName.getText().toString().length < 5){
            error = error + getString(R.string.error_first_name_incorrect_resgister_activity)

or you could also modify your regex so the user has to give atleast 5 letters using a quantifier( {5,} ). Then your code would look like this:

if(!lastName.getText().toString().matches(/\w{5,}/u)){
        error = error + getString(R.string.error_last_name_incorrect_resgister_activity) +"\n";

or

  if(!lastName.getText().toString().matches(/^[a-zA-Z\-_ ’'‘ÆÐƎƏƐƔIJŊŒẞÞǷȜæðǝəɛɣijŋœĸſßþƿȝĄƁÇĐƊĘĦĮƘŁØƠŞȘŢȚŦŲƯY̨Ƴąɓçđɗęħ‌​įƙłøơşșţțŧųưy̨ƴÁÀÂÄǍ‌​ĂĀÃÅǺĄÆǼǢƁĆĊĈČÇĎḌĐƊÐ‌​ÉÈĖÊËĚĔĒĘẸƎƏƐĠĜǦĞĢƔá‌​àâäǎăāãåǻąæǽǣɓćċĉčçď‌​ḍđɗðéèėêëěĕēęẹǝəɛġĝǧ‌​ğģɣĤḤĦIÍÌİÎÏǏĬĪĨĮỊIJĴ‌​ĶƘĹĻŁĽĿʼNŃN̈ŇÑŅŊÓÒÔÖ‌​ǑŎŌÕŐỌØǾƠŒĥḥħıíìiîïǐ‌​ĭīĩįịijĵķƙĸĺļłľŀʼnńn̈ň‌​ñņŋóòôöǒŏōõőọøǿơœŔŘŖ‌​ŚŜŠŞȘṢẞŤŢṬŦÞÚÙÛÜǓŬŪŨ‌​ŰŮŲỤƯẂẀŴẄǷÝỲŶŸȲỸƳŹŻŽ‌​Ẓŕřŗſśŝšşșṣßťţṭŧþúùû‌​üǔŭūũűůųụưẃẁŵẅƿýỳŷÿȳ‌​ỹƴźżžẓ]$/)){
            error = error + getString(R.string.error_last_name_incorrect_resgister_activity) +"\n";

I would suggest to use Character.isLetter(c) as below:

    char[] charArray= lastName.getText().toCharArray();

    for(Character c :charArray ){
        if(!Character.isLetter(c)){
            error = error + getString(R.string.error_last_name_incorrect_resgister_activity) +"\n";
            break;
        }
    }

Testing:

char c1= '-';
char c2= 'A';
char c3= 'ę';
char c4= 'ð';
char c5= 'ã';
boolean flag1 =Character.isLetter(c1);
boolean flag2 =Character.isLetter(c2);
boolean flag3 =Character.isLetter(c3);
boolean flag4 =Character.isLetter(c4);
boolean flag5 =Character.isLetter(c5);
System.out.println(flag1);
System.out.println(flag2);
System.out.println(flag3);
System.out.println(flag4);
System.out.println(flag5);

Result:

false
true
true
true
true

Edit:

Also if you need to consider digits as a valid characters(Not a special characters) then you need to use Character.isDigit(c) :

if(!Character.isLetter(c) && !Character.isDigit(c))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM