Question

I would like to stop the user from writing certain punctuations inside the editText box. So for example, if they used '!' or ';' they will receive a toast to alert them not to use it when they click on the Done button, but if they use other punctuations such as commas or periods, it's fine.

Most of the codes here are how to swap or remove, but I don't really see a code for detecting so... How do I create the code to detect these certain punctuations in an editText box?

Thanks in advance!

Was it helpful?

Solution 2

if(yourText.matches("[^;]+[^!]")){
    //Show the toast here
}

or

if(yourText.indexOf("!") != -1 || yourText.indexOf(";") != -1){
    //Show the toast here
}

For a reference I created this JSFiddle, but this is javascript.

OTHER TIPS

A simple solution would be to add android:digits attribute to <EditText> in layout xml file adding all allowed characters like.

 android:digits="ABCDE....Zabcde.....z012345789#$%^&*" 

Android will prevent any character not listed in android:digits from being entered.

The other solutions mentioned above are more elegant but you need to use a regular expression for all of them.

you should use an inputFilter

 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; 
    } 
 }; 

 edit.setFilters(new InputFilter[]{filter}); 

found this code..

You can use the TextWatcher, here is an example of usage:

mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                // TODO Auto-generated method stub
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                // TODO Auto-generated method stub
            }

            @Override
            public void afterTextChanged(Editable s) {

                // TODO Auto-generated method stub
            }
        });

InputFilter is a better solution in your case, as you can write all your restrictions at once place and use it at multiple places, any number of EditTexts.

Please see below:

 InputFilter[] filters = new InputFilter[1];
 filters[0] = new InputFilter(){
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
    if (end > start) {

        char[] acceptedChars = new char[]{'!',';'};

        for (int index = start; index < end; index++) {                                         
            if (new String(acceptedChars).contains(String.valueOf(source.charAt(index)))) { 
                //Do your thing; 
            }               
        }
    }
    return null;
}

};
searchEdit.setFilters(filters);

Source: How to restrict Edittext to some particular characters in android?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top