How to Restrict the Edittext to Accept Only Alphanumeric Characters

How to restrict the EditText to accept only alphanumeric characters

In the XML, add this:

 android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "

How to restrict TextView to allow only alpha-numeric characters in Android

In the XML, put this:

android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "

How to make your edit text alphanumeric? Android example

you can just check for them separately after the main check

if(!username.matches("[a-zA-Z0-9]+")){
Toast.makeText(this, "Username must be alphanumeric", Toast.LENGTH_SHORT).show();
return;
}

if(!username.matches("^.*[0-9].*$")){
Toast.makeText(this, "it should contain numbers", Toast.LENGTH_SHORT).show();
return;
}

if(!username.matches("^.*[a-zA-Z].*$")){
Toast.makeText(this, "it should contain letters", Toast.LENGTH_SHORT).show();
return;
}

EDIT:

To break down the regex:

^.*[0-9].*$ -->
^ <-- start of String

$ <-- end of String

.* match any (can also match empty string)

[0-9] match a number

you can also replace .* with [a-zA-Z0-9]* however, the first check will take care of that so its not really necessary.

EDIT 2

Another Approach is to check if it only contains letter or number: I prefer this one as its much more simplified, however it is essential to do the first check

if(!username.matches("[a-zA-Z0-9]+")){
Toast.makeText(this, "Username must be alphanumeric", Toast.LENGTH_SHORT).show();
return;
}

if(username.matches("[a-zA-Z]+")){ // only contains letters
Toast.makeText(this, "it should contain numbers", Toast.LENGTH_SHORT).show();
return;
}

if(username.matches("[0-9]+")){ //only contains numbers
Toast.makeText(this, "it should contain letters", Toast.LENGTH_SHORT).show();
return;
}

How to force EditText to accept only specific alphanumerical characters?

Try this, This will help you for sure.

 editText.setFilters(new InputFilter[] {new InputFilter.AllCaps()});

Validation allow only number and characters in edit text in android

Instead of using your "manual" checking method, there is something very easy in Android:

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)) &&
!Character.toString(source.charAt(i)).equals("_") &&
!Character.toString(source.charAt(i)).equals("-"))
{
return "";
}
}
return null;
}
};

edittext.setFilters(new InputFilter[] { filter });

Or another approach: set the allowed characters in the XML where you are creating your EditText:

<EditText 
android:inputType="text"
android:digits="0,1,2,3,4,5,6,7,8,9,*,qwertzuiopasdfghjklyxcvbnm,_,-"
android:hint="Only letters, digits, _ and - allowed" />

EditText to restrict entering special characters: single quote, double quote and emojis

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

How to restrict entry of numbers but allow all the special characters and alphabets in EditText?

Create a InputFilter.

EditText editText=findViewById(R.id.et);
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.isDigit(source.charAt(i))) {
return "";
}
}
return null;
}
};
editText.setFilters(new InputFilter[]{filter});

Edittext only allow letters (programmatically)

You can use this code below:

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.isLetter(source.charAt(i))&&!Character.isSpaceChar(source.charAt(i))) {
return "";
}
}
return null;
}
};
edit.setFilters(new InputFilter[] { filter });


Related Topics



Leave a reply



Submit