mum
mum

Reputation: 1645

How can limit input in EditText?

I have got a EditText. I only want input characters: a->z,A->Z,0->9 and @.#,-,_ How can limit key on keyboard input in EditText?

Upvotes: 1

Views: 1820

Answers (5)

phemt.latd
phemt.latd

Reputation: 1803

You can use this code snipset to check it with a regex:

EditText yourEditText = (EditText)findViewById(R.id.yourEditText );
if( !yourEditText.getText().toString().matches("[a-zA-Z]+") ); // This regex matches only letters
  yourEditText.setError( "Error on text, only a->z,A->Z" );

You can add this cose on an event...for example on a button click.

For the regex that you want you can find it on internet, or try to write your one and test it on this site: Regexplanet

Upvotes: 3

Rajesh Rajaram
Rajesh Rajaram

Reputation: 3277

Try this

<EditText 
  android:layout_height="wrap_content" 
  android:layout_width="match_parent" 
  android:inputType="text"
  android:digits="0123456789......yourcharcters"/>

Upvotes: 7

Aleksander Gralak
Aleksander Gralak

Reputation: 1509

Check this out: android:inputType

If you do not find good combination then implement your own listener and validate text each time it is changed.

Upvotes: 2

Janmejoy
Janmejoy

Reputation: 2721

you can try this

EditText et = new EditText(this);
int maxLength = 3;
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(maxLength);
et.setFilters(FilterArray);

Upvotes: 2

Alex Bonel
Alex Bonel

Reputation: 1424

The first idea coming to my head is to create a TextWatcher and validate every character inputed by user, using regex construction.

Upvotes: 2

Related Questions