Android: How can I validate EditText input? - android

I need to do form input validation on a series of EditTexts. I'm using OnFocusChangeListeners to trigger the validation after the user types into each one, but this doesn't behave as desired for the last EditText.
If I click on the "Done" button while typing into the final EditText then the InputMethod is disconnected, but technically focus is never lost on the EditText (and so validation never occurs).
What's the best solution?
Should I be monitoring when the InputMethod unbinds from each EditText rather than when focus changes? If so, how?

Why don't you use TextWatcher ?
Since you have a number of EditText boxes to be validated, I think the following shall suit you :
Your activity implements android.text.TextWatcher interface
You add TextChanged listeners to you EditText boxes
txt1.addTextChangedListener(this);
txt2.addTextChangedListener(this);
txt3.addTextChangedListener(this);
Of the overridden methods, you could use the afterTextChanged(Editable s) method as follows
#Override
public void afterTextChanged(Editable s) {
// validation code goes here
}
The Editable s doesn't really help to find which EditText box's text is being changed. But you could directly check the contents of the EditText boxes like
String txt1String = txt1.getText().toString();
// Validate txt1String
in the same method. I hope I'm clear and if I am, it helps! :)
EDIT: For a cleaner approach refer to Christopher Perry's answer below.

TextWatcher is a bit verbose for my taste, so I made something a bit easier to swallow:
public abstract class TextValidator implements TextWatcher {
private final TextView textView;
public TextValidator(TextView textView) {
this.textView = textView;
}
public abstract void validate(TextView textView, String text);
#Override
final public void afterTextChanged(Editable s) {
String text = textView.getText().toString();
validate(textView, text);
}
#Override
final public void beforeTextChanged(CharSequence s, int start, int count, int after) { /* Don't care */ }
#Override
final public void onTextChanged(CharSequence s, int start, int before, int count) { /* Don't care */ }
}
Just use it like this:
editText.addTextChangedListener(new TextValidator(editText) {
#Override public void validate(TextView textView, String text) {
/* Validation code here */
}
});

If you want nice validation popups and images when an error occurs you can use the setError method of the EditText class as I describe here

In order to reduce the verbosity of the validation logic I have authored a library for Android. It takes care of most of the day to day validations using Annotations and built-in rules. There are constraints such as #TextRule, #NumberRule, #Required, #Regex, #Email, #IpAddress, #Password, etc.,
You can add these annotations to your UI widget references and perform validations. It also allows you to perform validations asynchronously which is ideal for situations such as checking for unique username from a remote server.
There is a example on the project home page on how to use annotations. You can also read the associated blog post where I have written sample codes on how to write custom rules for validations.
Here is a simple example that depicts the usage of the library.
#Required(order = 1)
#Email(order = 2)
private EditText emailEditText;
#Password(order = 3)
#TextRule(order = 4, minLength = 6, message = "Enter at least 6 characters.")
private EditText passwordEditText;
#ConfirmPassword(order = 5)
private EditText confirmPasswordEditText;
#Checked(order = 6, message = "You must agree to the terms.")
private CheckBox iAgreeCheckBox;
The library is extendable, you can write your own rules by extending the Rule class.

Updated approach - TextInputLayout:
Google has recently launched design support library and there is one component called TextInputLayout and it supports showing an error via setErrorEnabled(boolean) and setError(CharSequence).
How to use it?
Step 1: Wrap your EditText with TextInputLayout:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/layoutUserName">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="hint"
android:id="#+id/editText1" />
</android.support.design.widget.TextInputLayout>
Step 2: Validate input
// validating input on a button click
public void btnValidateInputClick(View view) {
final TextInputLayout layoutUserName = (TextInputLayout) findViewById(R.id.layoutUserName);
String strUsername = layoutLastName.getEditText().getText().toString();
if(!TextUtils.isEmpty(strLastName)) {
Snackbar.make(view, strUsername, Snackbar.LENGTH_SHORT).show();
layoutUserName.setErrorEnabled(false);
} else {
layoutUserName.setError("Input required");
layoutUserName.setErrorEnabled(true);
}
}
I have created an example over my Github repository, checkout the example if you wish to!

This was nice solution from here
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++) {
String checkMe = String.valueOf(source.charAt(i));
Pattern pattern = Pattern.compile("[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789_]*");
Matcher matcher = pattern.matcher(checkMe);
boolean valid = matcher.matches();
if(!valid){
Log.d("", "invalid");
return "";
}
}
return null;
}
};
edit.setFilters(new InputFilter[]{filter});

I wrote a class that extends EditText which supports natively some validation methods and is actually very flexible.
Current, as I write, natively supported through xml attributes validation methods are:
alpha
alpha numeric
numeric
generic regexp
string emptyness
You can check it out here
Hope you enjoy it :)

I find InputFilter to be more appropriate to validate text inputs on android.
Here's a simple example:
How do I use InputFilter to limit characters in an EditText in Android?
You could add a Toast to feedback the user about your restrictions.
Also check the android:inputType tag out.

I needed to do intra-field validation and not inter-field validation to test that my values were unsigned floating point values in one case and signed floating point values in another. Here's what seems to work for me:
<EditText
android:id="#+id/x"
android:background="#android:drawable/editbox_background"
android:gravity="right"
android:inputType="numberSigned|numberDecimal"
/>
Note, you must not have any spaces inside "numberSigned|numberDecimal". For example: "numberSigned | numberDecimal" won't work. I'm not sure why.

This looks really promising and just what the doc ordered for me:
EditText Validator
public void onClickNext(View v) {
FormEditText[] allFields = { etFirstname, etLastname, etAddress, etZipcode, etCity };
boolean allValid = true;
for (FormEditText field: allFields) {
allValid = field.testValidity() && allValid;
}
if (allValid) {
// YAY
} else {
// EditText are going to appear with an exclamation mark and an explicative message.
}
}
custom validators plus these built in:
regexp: for custom regexp
numeric: for an only numeric field
alpha: for an alpha only field
alphaNumeric: guess what?
personName: checks if the entered text is a person first or last name.
personFullName: checks if the entered value is a complete full name.
email: checks that the field is a valid email
creditCard: checks that the field contains a valid credit card using Luhn Algorithm
phone: checks that the field contains a valid phone number
domainName: checks that field contains a valid domain name ( always passes the test in API Level < 8 )
ipAddress: checks that the field contains a valid ip address
webUrl: checks that the field contains a valid url ( always passes the test in API Level < 8 )
date: checks that the field is a valid date/datetime format ( if customFormat is set, checks with customFormat )
nocheck: It does not check anything except the emptyness of the field.

In main.xml file
You can put the following attrubute to validate only alphabatics character can accept in edittext.
Do this :
android:entries="abcdefghijklmnopqrstuvwxyz"

You can get desired behavior by listening when user hit "Done" button on keyboard, also checkout other tips about working with EditText in my post "Android form validation - the right way"
Sample code:
mTextView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
validateAndSubmit();
return true;
}
return false;
}});

for email and password validation try
if (isValidEmail(et_regemail.getText().toString())&&etpass1.getText().toString().length()>7){
if (validatePassword(etpass1.getText().toString())) {
Toast.makeText(getApplicationContext(),"Go Ahead".....
}
else{
Toast.makeText(getApplicationContext(),"InvalidPassword".....
}
}else{
Toast.makeText(getApplicationContext(),"Invalid Email".....
}
public boolean validatePassword(final String password){
Pattern pattern;
Matcher matcher;
final String PASSWORD_PATTERN = "^(?=.*[0-9])(?=.*[A-Z])(?=.*
[##$%^&+=!])(?=\\S+$).{4,}$";
pattern = Pattern.compile(PASSWORD_PATTERN);
matcher = pattern.matcher(password);
return matcher.matches();
}
public final static boolean isValidEmail(CharSequence target) {
if (target == null)
return false;
return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}

I have created this library for android where you can validate a material design EditText inside and EditTextLayout easily like this:
compile 'com.github.TeleClinic:SmartEditText:0.1.0'
then you can use it like this:
<com.teleclinic.kabdo.smartmaterialedittext.CustomViews.SmartEditText
android:id="#+id/passwordSmartEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:setLabel="Password"
app:setMandatoryErrorMsg="Mandatory field"
app:setPasswordField="true"
app:setRegexErrorMsg="Weak password"
app:setRegexType="MEDIUM_PASSWORD_VALIDATION" />
<com.teleclinic.kabdo.smartmaterialedittext.CustomViews.SmartEditText
android:id="#+id/ageSmartEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:setLabel="Age"
app:setMandatoryErrorMsg="Mandatory field"
app:setRegexErrorMsg="Is that really your age :D?"
app:setRegexString=".*\\d.*" />
Then you can check if it is valid like this:
ageSmartEditText.check()
For more examples and customization check the repository
https://github.com/TeleClinic/SmartEditText

Related

How do you make a field an email field not required in Android Saripaar?

I have a simple form in my Android app that I am using Android Saripaar to validate, one of the fields is an email address and according to the first example in the docs it should look something like this:
#NotEmpty
#Email
private EditText emailEditText;
However I would like it to be an optional field so I omitted the #NotEmpty annotation:
#Email
private EditText emailEditText;
But when I leave it empty it understandably marks it as an invalid email address. Is it possible to have this field be optional without writing a custom rule?
Since we needed an optional email field for a project I went ahead and made a rule/annotation combination according to this answer. Referred to Saripaar docs for Rules and Annotations.
OptionalEmailRule.java
public class OptionalEmailRule extends AnnotationRule<OptionalEmail, String> {
protected OptionalEmailRule(final OptionalEmail email) {
super(email);
}
#Override
public boolean isValid(final String email) {
if(TextUtils.isEmpty(email)){
//email is empty and therefore valid
return true;
}
else{
//email is not empty, proceed as usual
boolean allowLocal = mRuleAnnotation.allowLocal();
return EmailValidator.getInstance(allowLocal).isValid(email);
}
}
}
OptionalEmail.Java
#ValidateUsing(OptionalEmailRule.class)
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.FIELD)
public #interface OptionalEmail {
boolean allowLocal() default false;
int sequence() default -1;
int messageResId() default -1;
String message() default "Invalid email";
}
Inside Validation Activity
Register the Annotation
Validator.registerAnnotation(OptionalEmail.class);
Annotate the field
#OptionalEmail
public EditText editEmail;

How to create a preference that accepts only integer values

Is there a way to create a preference in a PreferenceFragment that accepts only integer values? I could implement an EditTextPreference and register an OnPreferenceChangeListener in which I could reject the change if the user enters a a string that is not a number, but I would prefer something that is meant for holding only numbers and that does not allow users to enter anything else, maybe showing only a dial pad keyboard.. I don't such a preference exist, since every descendant of Preference is mapped onto a Boolean (CheckBoxPreference), a String (*EditTextPreference) or a String array (MultiSelectListPreference), i.e. there are no preferences mapped onto integers, but maybe some of you can give me an hint or at least tell me if there are better solutions than the one I've proposed above.
Solution proposed by Grey:
EditText editText = ((EditTextPreference)
findPreference("intent_property")).getEditText();
editText.setKeyListener(new NumberKeyListener() {
#Override
public int getInputType() {
// The following shows the standard keyboard but switches to the view
// with numbers on available on the top line of chars
return InputType.TYPE_CLASS_NUMBER;
// Return the following to show a dialpad as the one shown when entering phone
// numbers.
// return InputType.TYPE_CLASS_PHONE
}
#Override
protected char[] getAcceptedChars() {
return new String("1234567890").toCharArray();
}
});
Shorter solution, which does not allow varying the keyboard to dialpad but requires less code:
EditText editText = ((EditTextPreference)
findPreference("intent_property")).getEditText();
editText.setKeyListener(new DigitsKeyListener());
I don't like just one thing about this solution: the user can enter 0000 and it is accepted and saved in the shared preference (which is a String) as "0000".. this requires you to implement a OnSharedPreferenceChangeListener to intercept changes to shared preferences and remove leading zeros in this preference or implement a change listener directly on this preference and return false to refuse numbers with trailing zeros (tell me if there is a better solution to this last problem which does not involve implementing your own Preference). It would be beautiful if we could modify the newValue in OnPreferenceChangeListener..
There is an easier way to do this. Just set in the EditTextPreference in your XML to android:numeric="integer" (you can set it to integer, signed or decimal).
Eclipse (or whatever tool you are working in) won't show you this as a possible attribute, but just write it in your EditTextPreference and clean your project. You will see that it won't give you any errors. :)
After reading the proposed solutions, I still think a custom preference is the way to go. I did read your remark in bold text, but setting up a basic EditTextIntegerPreference is actually super simple and it will solve the remaining issue you have when the user enters for example "0000".
Just a note up front: since you normally want to be able to use preferences in several places in your app, as far as I'm concerned, a proper implementation of an EditTextIntegerPreference would store its value against an Integer. That way you'll be able to retrieve the int value anywhere, without the need to first parse or cast it.
However, to keep this answer to the point and compact, I'm actually going to show an extension of a regular EditTextPreference, which means that under the hood, the value will still be stored as a string. If you're really keen on getting the 'proper' implementation to work, I don't mind writing that up later on. It shouldn't be too tricky though, so you might want to have a crack at it yourself first. :)
public class EditTextIntegerPreference extends EditTextPreference {
private Integer mInteger;
public EditTextIntegerPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
}
public EditTextIntegerPreference(Context context, AttributeSet attrs) {
super(context, attrs);
getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
}
public EditTextIntegerPreference(Context context) {
super(context);
getEditText().setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_SIGNED);
}
#Override public void setText(String text) {
final boolean wasBlocking = shouldDisableDependents();
mInteger = parseInteger(text);
persistString(mInteger != null ? mInteger.toString() : null);
final boolean isBlocking = shouldDisableDependents();
if (isBlocking != wasBlocking) notifyDependencyChange(isBlocking);
}
#Override public String getText() {
return mInteger != null ? mInteger.toString() : null;
}
private static Integer parseInteger(String text) {
try { return Integer.parseInt(text); }
catch (NumberFormatException e) { return null; }
}
}
The reason why this solves the "0000" issue, is that we're simply casting the user typed value to an int before we store it, and plug it back into a string upon retrieval. That means any leading zeroes (except for '0' as a number of course) will automagically disappear.
The 'number' input type will restrict the user to input only enter numbers, so the parseInteger(...) method is mainly present for sanity reasons, although it will catch a NumberFormatException if you try to enter an empty or null string. Again, you can make this more pretty, which I haven't done for now.
i guess u can call getEditText() to get a EditText obj , then call setKeyListener with a NumberKeyListener.
You should use PreferenceActivity which is basically used to show preferences like an xml file. In this activity you can use checkboxes, edit text and save values to preferences.
You can also reopen the dialog when you get an exception after trying to parseInt() the number.
Here is a sample code:
public class MyEditTextPreference extends EditTextPreference {
private EditText _editText;
public MyEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
_editText = getEditText();
}
#Override
public void onDismiss(DialogInterface dialog) {
super.onDismiss(dialog);
try {
Integer.parseInt(_editText.getText().toString());
} catch (NumberFormatException e) {
((Dialog) dialog).show();
}
}
}

Input validation in Android, Is declaring an InputType enough

I have several fields in my activity which all take integers, and integers only.
Looking around the SO here I see that the easiest way to perform validation is to simply declare an InputType of Integer in my layout.xml
This works fine. User can only enter numbers and my business logic is happy. It can also handle blank fields.
However, I was wondered is there any possible way a user can input a non numeric value? Do I need to be able to handle this? If the answer is yes then it means I will need to update all my unit tests along with the field validation in my application but would prefer to trust Android OS to do it.
If you use InputType correctly, the user won't be able to enter any characters that are not specified by that InputType.
if you're OC'ed about this, you can use InputFilter to prevent invalid characters from being entered/pasted into the EditText.
InputFilter filter = new InputFilter() {
#Override
public CharSequencefilter(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;
}
}

Validating edittext in Android

I'm new to android & I'm trying to write an application for a project.
I need to check whether the user has entered 7 numbers followed by one alphabet in edittext.
Example: 0000000x
How should I do that? TIA! :)
Probably the best approach would be to use a TextWatcher passed into the addTextChangedListener() method of the EditText. Here is an example use:
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable e) {
String textFromEditView = e.toString();
validateText(textFromEditView);
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//nothing needed here...
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//nothing needed here...
}
});
I will leave the implementation of the validateText(String) method as an exercise for the reader, but I imagine it should be easy enough. I would either use:
A simple Regular Expression.
Or since this case is easy enough, checking that the length of the string is 8, and reviewing each character. There is a simple utility class to inspect the characteristics of characters. Character.isDigit(char) and Character.isLetter(char)
OnKeyListener listens to every key stroke in the view. you can use that to check whether the user has entered what he is supposed.
eg : if the no of char entered is 7 then
check if it follows the reqd expression format.
There is a Class called Pattern in Android in that you can give Regular Expression to match your Requirements try this follwoing code i think it may work
Pattern p = Pattern.compile( "{7}" );
Matcher m = p.matcher(String.valueOf(edittext));
This will be true only if 7 characters are there in the Text box and then you can use some menthods like "Character.isDigit(char) and Character.isLetter(char)"

Android: Expand the inputType of EditText?

I'd like to allow possible inputs of 0-9, "," or "-" for EditText and couldn't find an answer.
I know using the "|" as an separator is possible:
android:inputType="number|text"
But how can I archive something like this (too bad it doesn't work):
android:inputType="number|,|-"
Does anyone know?
Regards,
cody
Addition to the comment below:
private class MyKeylistener extends
NumberKeyListener {
public int getInputType() {
return InputType.TYPE_CLASS_NUMBER;
}
#Override
protected char[] getAcceptedChars() {
return new char[] {'0','1','2','3','4','5','6','7','8','9',',','-'};
}
}
I don't think there is a way to provide an arbitrary filter like that. You have to use the provided filter types.
If you can't find a combination of those types that does what you want, you probably need to do the filtering yourself using a TextWatcher or InputFilter on the EditText.

Categories

Resources