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});
How to restrict the EditText to accept only alphanumeric characters
In the XML, add this:
android:digits="abcdefghijklmnopqrstuvwxyz1234567890 "
How can restrict my EditText input to some special character like backslash(/),tild(~) etc by soft keyboard in android programmatically
Try this may work for you
public class MainActivity extends Activity {
private EditText editText;
private String blockCharacterSet = "~#^|$%&*!";
private InputFilter filter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (source != null && blockCharacterSet.contains(("" + source))) {
return "";
}
return null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
editText.setFilters(new InputFilter[] { filter });
}
}
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.
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" />
Android : Allow User to add only alphabets and special characters in an EditText
I used a InputFilter to block out only numbers :
private static InputFilter filter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
String blockCharacterSet = "1234567890";
if (source != null && blockCharacterSet.contains(("" + source))) {
return "";
}
return null;
}
};
Then Applying the filter :
edittext.setFilters(new InputFilter[] { filter });
how to block the special character and smaller case characters in android EditText?
android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
android:inputType="textCapCharacters"
android:capitalize="sentences"
use the above line in the.xml file so that it only enters the alphabets and numbers only
How to restrict special characters from an Android EditText field?
Have you tried adding the android:digits="abcde.....0123456789"
attribute?
Although the android:digits
specify that it is a numeric field, it does work for me to set it to accept letters as well, and special characters as well (tested on SDK-7).
If this doesn't work then you'll have to implement KeyListener
see: http://developer.android.com/reference/android/widget/TextView.html#setKeyListener(android.text.method.KeyListener)
How to create EditText accepts Alphabets only in android?
EditText state = (EditText) findViewById(R.id.txtState);
Pattern ps = Pattern.compile("^[a-zA-Z ]+$");
Matcher ms = ps.matcher(state.getText().toString());
boolean bs = ms.matches();
if (bs == false) {
if (ErrorMessage.contains("invalid"))
ErrorMessage = ErrorMessage + "state,";
else
ErrorMessage = ErrorMessage + "invalid state,";
}
Related Topics
How to Convert Android.Media.Image to Bitmap Object
Android Push Notifications Not Being Received When App Closed
Android.Support.V7.App.Activitycompat Not Found
How to Convert Image to Zpl Code for Printing Using Zebra Printer
How to Emulate Gps Location in the Android Emulator
How to Select a Video from the Gallery and Get It's Real Path
Android Scrollview Not Scrolling Down After Keyboard Opens
Android Refresh a Fragment List from Its Parent Activity
How to Make Primary Key as Autoincrement for Room Persistence Lib
How to Set the Starting Position of a Scrollview
How to Use Adb With Emulator Like Memu/Bluestacks/Nox App Player
Unable to Add Window - Token Null Is Not Valid; Is Your Activity Running
How to Send a Firebase Notification to All Devices Via Curl
Setting Webview to View Desktop Site and Not Mobile Site
Move to Another Edittext When Soft Keyboard Next Is Clicked on Android