Question Detail

In my Registration form Name, field (EditText) accepting Number values but Name can't be a Number. How to restrict Edit Text to restrict to get only Text Values and denial Number Value? Numbers should not enter in Name Field.

Please suggest any Working Example for Restrict EditText to only get Text.

 

I have tried below code but not working 

<EditText
    android:id="@+id/name_EditText_signup_ID"
    style="@style/edittextStyle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/top_right_round_edge_input"
    android:hint="Full Name"
    android:inputType="textPersonName"
    android:padding="@dimen/fieldPadding"/>


Thread Reply

Hemant Sharma

- 9 months ago

Try this Solution:

<EditText
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/top_right_round_edge_input"
    android:hint="Full Name"
    android:inputType="textPersonName"
    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
/>

if you want to set EditText Input value programmatically in your Java file / From Code:

editTextView.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"));

Hemant Sharma

- 9 months ago

Try this Solution:

<EditText
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/top_right_round_edge_input"
    android:hint="Full Name"
    android:inputType="textPersonName"
    android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
/>

if you want to set EditText Input value programmatically in your Java file / From Code:

editTextView.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"));

alex levine

- 8 months ago

This is also working but I am not able to add space to

editTextView.setKeyListener(DigitsKeyListener.getInstance("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"));

 

For deny Numbers and accept the only Alphabet in EditText I have to use setFilters 

name_EditText_profile.setFilters(new InputFilter[]{acceptonlyAlphabetValuesnotNumbersMethod()});

 

//Accept Only Alphabet in EditText
public static InputFilter acceptonlyAlphabetValuesnotNumbersMethod() {
    return new InputFilter() {

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

            boolean isCheck = true;
            StringBuilder sb = new StringBuilder(end - start);
            for (int i = start; i < end; i++) {
                char c = source.charAt(i);
                if (isCharAllowed(c)) {
                    sb.append(c);
                } else {
                    isCheck = false;
                }
            }
            if (isCheck)
                return null;
            else {
                if (source instanceof Spanned) {
                    SpannableString spannableString = new SpannableString(sb);
                    TextUtils.copySpansFrom((Spanned) source, start, sb.length(), null, spannableString, 0);
                    return spannableString;
                } else {
                    return sb;
                }
            }
        }

        private boolean isCharAllowed(char c) {
            Pattern pattern = Pattern.compile("^[a-zA-Z ]+$");
            Matcher match = pattern.matcher(String.valueOf(c));
            return match.matches();
        }
    };
}