2

I have a problem while inserting special characters: single quote, double quote and emojis into the database from editText.

I want my editText to restrict those characters, and I was successful in restricting emojis, but I failed to restrict users entering single quote and double quote.

E.g. When I try to enter text Today's List from editText into the database it generates an exception.

I have used InputFilter in editText to filter emojis, and I want this filter to restrict single quote and double quotes too.

public static InputFilter getEditTextFilterEmoji() {
    return new InputFilter() {

        @Override
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

            CharSequence sourceOriginal = source;
            source = replaceEmoji(source);
            end = source.toString().length();

            if (end == 0)
                return ""; //Return empty string if the input character is already removed

            if (!sourceOriginal.toString().equals(source.toString())) {
                char[] v = new char[end - start];
                TextUtils.getChars(source, start, end, v, 0);

                String s = new String(v);

                if (source instanceof Spanned) {
                    SpannableString sp = new SpannableString(s);
                    TextUtils.copySpansFrom((Spanned) source, start, end, null, sp, 0);
                    return sp;
                } else {
                    return s;
                }
            } else {
                return null; // keep original
            }
        }

        private String replaceEmoji(CharSequence source) {

            String notAllowedCharactersRegex = "[^a-zA-Z0-9@#\\$%\\&\\-\\+\\(\\)\\*;:!\\?\\~`£\\{\\}\\[\\]=\\.,_/\\\\\\s'\\\"<>\\^\\|÷×]";
            return source.toString()
                    .replaceAll(notAllowedCharactersRegex, "");
        }
    };
}

Can anyone help me with this ?

David Buck
  • 3,752
  • 35
  • 31
  • 35
swetabh suman
  • 1,949
  • 2
  • 19
  • 24
  • Possible duplicate of [How do I use InputFilter to limit characters in an EditText in Android?](http://stackoverflow.com/questions/3349121/how-do-i-use-inputfilter-to-limit-characters-in-an-edittext-in-android) – Manoj Perumarath Jan 06 '17 at 05:53
  • Possible duplicate of [How to exclude special characters from android keypad for EditText](http://stackoverflow.com/questions/7335455/how-to-exclude-special-characters-from-android-keypad-for-edittext) – Ankita Shah Jan 06 '17 at 06:25
  • Consider picking an answer if it helped. – W4R10CK Jan 08 '17 at 19:06

7 Answers7

4

Restrict the EditText to use given digits only.

            <EditText
            ........
            android:inputType="textPersonName"
            android:digits="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyY zZ0123456789"
            />
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
4

As per my understanding all emoji contains /u so I used it like this

public static InputFilter[] getEmojiFilter(String blockChars) {
    return new InputFilter[]{(source, start, end, dest, dstart, dend) -> {
        String source1= StringEscapeUtils.escapeJava(source.toString());
        for (int i = start; i < end; i++) {
            if (source != null && blockChars.contains("" + source1.charAt(i))) {
                return source.subSequence(start, i);
            }
        }
        return null;
    }};
}

and Write below line for Edittext

editext.setFilters(getEmojiFilter("'\"\\//\\u"));
Deepak
  • 127
  • 4
  • 11
1

If you want alphabets only in edittext, Add this line in edittext tag

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
Sebastian
  • 417
  • 3
  • 13
1

Try this one....worked for me

private EditText InputText;
private String blockCharacterSet = "~#^|$%&*!"; //Special characters to block

private InputFilter filter = new InputFilter() {

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

        if (source != null && blockCharacterSet.contains(("" + source))) {
            return "";
        }
        return null;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    InputText = (EditText) findViewById(R.id.InputText);
    InputText.setFilters(new InputFilter[] { filter });
}
laughingmn
  • 11
  • 3
0

Add This in Strings.xml. It's allow u to enter alphanumeric and space also.

<string name="charset">0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY Z</string>
0

This answer worked for me. I searched different questions and I got my answer.

Community
  • 1
  • 1
swetabh suman
  • 1,949
  • 2
  • 19
  • 24
0

setFilters(new InputFilter[]{new EmojiExcludeFilter()}); private class EmojiExcludeFilter implements InputFilter {

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        String specialChars = "/*!@#$%^&*()\"{}_[]|\\?/<>,.:-'';§£¥...";
        for (int i = start; i < end; i++) {
            int type = Character.getType(source.charAt(i));
            if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL || type == Character.MATH_SYMBOL || specialChars.contains("" + source)||  Character.isWhitespace(0)) {
                return "";
            }
        }
        return null;
    }
}
karthik
  • 347
  • 1
  • 9