Android: How can I validate EditText input?
Asked Answered
P

14

179

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?

Pellicle answered 4/5, 2010 at 5:35 Comment(4)
Do you really need to validate the EditText input at the same time the user is typing? Why don't you just validate the EditText's once the user click on the Done button?Acetyl
That's exactly what I want: for the text to be verified when the user clicks the Done button (by Done button I mean the "Done" button on the QWERTY InputManager...NOT the form's submit button). Except that when I hit the Done button, focus stays on the last element in the form, and my validation method is never triggered. Hope my wording is clear...Pellicle
@Acetyl 's solution is exactly what I was looking for and is found here: #43014312Priscillaprise
@Acetyl Coming a bit late, but I'm looking for a solution where the EditText's are validate while the person is typing. I have a Login/Register form and I want to show the "Submit" button only when the form data is valid.Flinn
W
160

Why don't you use TextWatcher ?

Since you have a number of EditText boxes to be validated, I think the following shall suit you :

  1. Your activity implements android.text.TextWatcher interface
  2. You add TextChanged listeners to you EditText boxes
txt1.addTextChangedListener(this);
txt2.addTextChangedListener(this);
txt3.addTextChangedListener(this);
  1. 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.

Willing answered 18/5, 2010 at 9:28 Comment(9)
That looks like exactly what I need. Hadn't heard of TextWatcher (new to the SDK/API), but I'll test it out and see if it behaves the way I think it will. Thanks for the info!Pellicle
you're welcome! :) now that you're validating it, could you share how are you going to inform the user of the validation failure? I'm currently looking for best methods for the same.Willing
Nikhil Patil, I just use Toast to let the user know they've done something wrong. Is there some reason why that won't be effective in your case?Schonthal
Of course, Toast is a natural way on android. But when we have considerable amount of elements on screen which need validation, toasts don't seem to be the correct choice.(IMHO,It would annoy the user) I have been experimenting with TextView.setError() (developer.android.com/reference/android/widget/…Willing
Although there is poor support on TextWatcher, it works... kinda!Woaded
What if you want to replace the EditText with a default value on validation fail? Changing inside one of these functions would cause it to run again, or worse, forever if the default value itself wasn't valid. Am I correct in this thinking?Orwin
@mattblang I think afterTextChanged is only triggered if a user interaction changes the text. So programmatic-ally setting it shouldn't create a problem. But this is just a speculation. Try it out and let us know?Willing
@NikhilPatil java.lang.StackOverflowErrorOrwin
@mattblang So you are right! It will be a very bad idea to use this if one also wants to set the default value.Willing
P
135

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 */
    }
});
Pomerleau answered 7/8, 2012 at 3:10 Comment(8)
Once we're speaking of validation, you mean EditText, I guess?Ingeborg
@fremmedehenvendelser : every EditText IS-A TextViewWilling
I like this solution. But, when I try implementing it, I get this error: "Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.addTextChangedListener(android.text.TextWatcher)' on a null object reference"Mclemore
@Mclemore most probably you didn't initialize your EditText. Make sure you're calling addTextChangedListener after resolving your edittext from the viewHorowitz
How can you do a new TextValidator() if it is an abstract class ?Ankney
@StephaneEybert It's an anonymous classPomerleau
The ButterKnife library might be of interest to anyone looking to write shorter code.Standifer
Interface segregation principle in practiceHenton
C
100

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

Screenshot of the use of setError taken from Donn Felker, the author of the linked post

Checkerbloom answered 23/11, 2011 at 20:59 Comment(1)
How do you get a TextWatcher to access two EditTexts? I've successfully added a TextWatcher to my passwordConfirmTextField, but I need to reference the other passwordTextField, so I can compare them. Any suggestions?Flinn
P
27

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.

Pickaback answered 23/10, 2012 at 14:6 Comment(3)
This library works like a charm. But the @TextRule annotations was removed from version 2.0.3?Housebreaking
It has been replaced with the @Length annotation.Pickaback
@RagunathJawahar I have noted that validation doesn't work if You validate incoming data , I.e. contact , so I am trying to validate Email which has came from Intent -> Contacts , but once I focus on EditText and add / delete any text then validation works as validation is also being called on TextChange and validate() is also called when we receive data from Contact.Abm
P
14

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!

Petulancy answered 25/7, 2015 at 10:16 Comment(1)
Best answer, but I had to use com.google.android.material.textfield.TextInputLayout (notice the material change). Got it from this answer: https://mcmap.net/q/137990/-android-support-design-widget-textinputlayout-could-not-be-instantiatedChop
E
11

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}); 
Embraceor answered 14/8, 2012 at 8:16 Comment(1)
how do i use it along with space and limit no two space next to each other?Volution
T
8

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:

  1. alpha
  2. alpha numeric
  3. numeric
  4. generic regexp
  5. string emptyness

You can check it out here

Hope you enjoy it :)

Tumescent answered 24/5, 2012 at 21:44 Comment(0)
C
7

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.

Cru answered 5/10, 2011 at 8:49 Comment(3)
This is a nice solution for things that can be validate as you type (alpha numeric input), but it would not work for things that should only be validate once the user is done entering the input (email address).Principe
How would you trigger that Toast? The filter prevents any textwatchers from reacting... Perhaps with an onKeyListener?Disentomb
I triggered that Toast with an IF condition from the filter() method (in InputFilter class).Cru
P
6

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.

Principle answered 1/1, 2011 at 23:50 Comment(0)
S
5

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.
Szechwan answered 18/12, 2013 at 7:36 Comment(0)
M
2

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;
    }});  
Marie answered 4/8, 2013 at 21:33 Comment(0)
E
2

In main.xml file

You can put the following attrubute to validate only alphabatics character can accept in edittext.

Do this :

  android:entries="abcdefghijklmnopqrstuvwxyz"
Extrabold answered 7/4, 2014 at 4:53 Comment(0)
T
0

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();
}
Tawnatawney answered 10/5, 2018 at 7:4 Comment(0)
E
-2

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

Elatia answered 22/12, 2017 at 9:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.