Validation Allow Only Number and Characters in Edit Text in Android

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" />

How to restrict the EditText to accept only alphanumeric characters

In the XML, add this:

 android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "

Android : How to set acceptable numbers and characters in EditText?

Android provides an easy way to edit text fields by modifying the layout xml and adding an android:inputType="text". This lets you easily create some basic validations like numbers, decimal, phone or emails. But there's no parameter for alphanumeric (i.e. no special characters). To do this, you need to use an input filter like below and set the fields you want to validate with that filter in code. This input filter

 InputFilter alphaNumericFilter = new InputFilter() {   
@Override
public CharSequence filter(CharSequence arg0, int arg1, int arg2, Spanned arg3, int arg4, int arg5)
{
for (int k = arg1; k < arg2; k++) {
if (!Character.isLetterOrDigit(arg0.charAt(k))) {
return "";
} //the first editor deleted this bracket when it is definitely necessary...
}
return null;
}
};
mFirstName.setFilters(new InputFilter[]{ alphaNumericFilter});

Android Edittext Validate for all digits

There is an in-built function in TextUtils class to check if the string contains only digits. You could use that.

if (TextUtils.isDigitsOnly(your_string)) {
//show error message
return;
}

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

In the XML, put this:

android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "

How to force EditText to accept only numbers?

Use android:inputType="number" in your layout XML

edittext validation which will only allow number and : symbol

Use android:digits attribute for filtering symbols:

<EditText
android:digits="0123456789:"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

or programmatically:

 editText.setKeyListener(DigitsKeyListener.getInstance("0123456789:"));

Making an EditText field accept only letters and white spaces in Android

Try this:

EditText yourEditText = (EditText) findViewById(R.id.yourEditText);
yourEditText.setFilters(new InputFilter[] {
new InputFilter() {
@Override
public CharSequence filter(CharSequence cs, int start,
int end, Spanned spanned, int dStart, int dEnd) {
// TODO Auto-generated method stub
if(cs.equals("")){ // for backspace
return cs;
}
if(cs.toString().matches("[a-zA-Z ]+")){
return cs;
}
return "";
}
}
});

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;
}


Related Topics



Leave a reply



Submit