How to check if the user typed any special characters in EditText?

0

How can I check if the user entered any special characters (*, /, +, & amp ;, etc.) other than if(srt.contains("@") || srt.contains("!") || ... ?

Is there any more practical way?

And if he typed, what do I do to "lock" this typing?

    
asked by anonymous 01.04.2015 / 21:56

2 answers

1

Try this:

if ("teste".indexOf("$") >= 0)   
   // Tem $  
else  
   // Não tem $ 

You can put filter too, like this:

InputFilter filter = new InputFilter()
{
    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)
    {
        if (source.toString().contains("@"))
            return "";
        else
            return null; // Accept original replacement.
    }    
};

editText.setFilters(new InputFilter[] { filter });
    
01.04.2015 / 21:59
0

Similar to William's response, however, filtering only to letters or numbers.

InputFilter filter = new InputFilter() {
    public CharSequence filter(CharSequence source, int start, int end,
            Spanned dest, int dstart, int dend) {
        for (int i = start; i < end; i++) {
            if (!Character.isLetterOrDigit(source.charAt(i))) {
                return "";
            }
        }
        return null;
    }
};
seuEditTxt.setFilters(new InputFilter[] { filter });
    
05.06.2015 / 21:38