0

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!

Triode
  • 11,309
  • 2
  • 38
  • 48
Fuchsia
  • 77
  • 1
  • 7
  • http://stackoverflow.com/questions/10344493/android-how-to-set-acceptable-numbers-and-characters-in-edittext – Pararth Feb 18 '14 at 18:06

5 Answers5

2

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.

bhaskarc
  • 9,269
  • 10
  • 65
  • 86
1
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.

Triode
  • 11,309
  • 2
  • 38
  • 48
  • Hi, thanks for replying! However it says 'invalid character constant' for this part....: '[^;]+[^!]' – Fuchsia Feb 18 '14 at 18:11
  • thanks! I cant get it to work at the moment... but maybe cos I need to sleep... I'll try again tmr, thank you – Fuchsia Feb 18 '14 at 18:21
  • But you can accept any of these answer if you found it help full, at least pay attention towards their effort in helping you. – Triode Feb 18 '14 at 18:24
0

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..

reidisaki
  • 1,525
  • 16
  • 29
  • Hrmm... Does this allow me to choose some punctuations and not others? Where would I put them? – Fuchsia Feb 18 '14 at 17:57
  • This is where you want to check for certain punctuations: //disallow ? and *.. you can put whatever you want there. if(source.charAt(i) == '?' || source.charAt(i) == "*") { return ""; } if (!Character.isLetterOrDigit(source.charAt(i))) { return ""; } – reidisaki Feb 18 '14 at 21:55
  • Thank you, I'm still in the middle of trying to see which one is the best for me :) – Fuchsia Feb 19 '14 at 16:30
0

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
            }
        });
Pete
  • 3,842
  • 3
  • 31
  • 42
  • I don't quite get it... >_< Where do I put the selected punctuations that I don't want the user to use, and how do I write it please? – Fuchsia Feb 18 '14 at 18:03
  • In the onTextChanged event, add logic that will check if the characters exist in the string, and display a Toast if necessary. Please read the documentation I linked to for further information about the arguments passed into the event handler. – Pete Feb 18 '14 at 18:10
0

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?

Community
  • 1
  • 1
Atul O Holic
  • 6,692
  • 4
  • 39
  • 74