shivcena
shivcena

Reputation: 2053

edit text for name

I have an edited text getting name as user input. I need to restrict all special characters except dot(.). How to do this? Refer my code below

EditText Name= new EditText(this);
Name.setLayoutParams(new TableRow.LayoutParams(dp(220),dp(40)));
Name.setHorizontallyScrolling(true);        
Name.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
Name.setInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME);
Name.setTypeface(Typeface.DEFAULT);
Name.setFilters(new InputFilter[]{new InputFilter.AllCaps()});

Upvotes: 1

Views: 1596

Answers (4)

Vishal Pawar
Vishal Pawar

Reputation: 4340

use inputType here is the link for doc refer it

inputType="textPersonName"

http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType use

NameEdt.setRawInputType(InputType.TYPE_TEXT_VARIATION_PERSON_NAME); 

Upvotes: 3

Juned
Juned

Reputation: 6336

NumberKeyListener PwdkeyListener = new NumberKeyListener() {

public int getInputType() {
return InputType.TYPE_MASK_VARIATION;
}

    @Override
    protected char[] getAcceptedChars() {
    return new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 
                    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '@', '_', '#', '$', '%', '&', '*', '-', '+', '(', ')', '!', '"', '\'', ':', 
                    ';', '/', '?', ',', '~', '`', '|', '\\', '^', '<', '>', '{', '}', '[', ']', '=', '£', '¥', '€', '¢', '•','©' };
    }
};

edtObj.setKeyListener(PwdkeyListener);

See this for more info Android - Want to restrict some charaters to the edittext

Upvotes: 1

Naveen Kumar
Naveen Kumar

Reputation: 3818

try this

EditText editText = (EditText)findViewById(R.id.editText);
        InputFilter[] filters = new InputFilter[1];
        filters[0] = new InputFilter(){
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                if (end > start) {

                    char[] acceptedChars = new char[]{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 
                            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
                            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.'};

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

        };
        editText.setFilters(filters);

Upvotes: 2

Hardik Joshi
Hardik Joshi

Reputation: 9507

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[]{'a','b'};

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

    };
    Name.setFilters(filters);

Upvotes: 1

Related Questions