How to detect special characters in an edit text and display a Toast in response (Android)?

前端 未结 6 1849
既然无缘
既然无缘 2021-01-01 01:06

An apology for my bad English, I\'m using google translate.

I\'m creating an activity in which users must create a new profile. I put a limit to edit text of 15 char

6条回答
  •  猫巷女王i
    2021-01-01 01:27

    If you want to pick up the special chars and support more than just the very resrictive set of English alpha bet and numbers:

    String edit_text_name = YourEditTextName.getText().toString();
    
    Pattern regex = Pattern.compile("[$&+,:;=\\\\?@#|/'<>.^*()%!-]");
    
    if (regex.matcher(edit_text_name).find()) {
            Log.d(TAG, "SPECIAL CHARS FOUND");
            //handle your action here toast message/ snackbar or something else
            return;
    }
    

提交回复
热议问题