11

I have a registration form which I need to validate before submit. The form has the following fields:name,email, contact number and password. I need the name to have a value, the email to have the correct format,contact number should be numbers at least 10 numbers and the password to be at least 6 characters.

user733936
  • 111
  • 1
  • 1
  • 3
  • search the site for "input validation" select any of the thousands of answers – Eric May 02 '11 at 05:26
  • Agree with @Shashank_Itmaster and to limit the user to a particular number of characters in `EditText` you can use the attribute [android:maxLength](http://developer.android.com/reference/android/widget/TextView.html#attr_android:maxLength). – Mudassir May 02 '11 at 05:26

6 Answers6

9

try this

vUsername = etUsername.getText().toString();
vFirstname = etFirstname.getText().toString();
vEmail = etEmail.getText().toString(); 
vPwd = etPwd.getText().toString();
vCpwd = etCpwd.getText().toString();   

if("".equalsIgnoreCase(vUsername) //vUsername.equalsIgnoreCase("") could lead to NPE
   || "".equalsIgnoreCase(vFirstname)
   || "".equalsIgnoreCase(vEmail)
   || "".equalsIgnoreCase(vPwd)
   || "".equalsIgnoreCase(vCpwd) )
{
    Toast.makeText(userRegistration.this, "All Fields Required.", 
         Toast.LENGTH_SHORT).show();
}
checkemail(vEmail);
if(emailcheck==true)
{
    // your code here
}

public void checkemail(String email)
{
    Pattern pattern = Pattern.compile(".+@.+\\.[a-z]+");
    Matcher matcher = pattern.matcher(email);
    emailcheck = matcher.matches();
}
Shubham A.
  • 2,446
  • 4
  • 36
  • 68
5

Alternatively, you can use a validation library to perform your validations on Android. It is driven by annotation and thereby it reduces a lot of boiler-plate code. Your use case when solved using this app would look like the following:

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

There is a dearth of documentation now but the annotation example on the home page should get you started. You can also read this blog on how to create custom rules in case the stock rules do not fit your needs.

PS: I am the author of this library.

Ragunath Jawahar
  • 19,513
  • 22
  • 110
  • 155
  • for some reason, the error on the 1st edittext does not clear - any thoughts? – jramoyo Aug 07 '13 at 17:31
  • @jramoyo, I think this is specifically related to the version of Android and not the framework. I've come across similar comments before and we found that it has nothing to do with the framework itself. Could you please let me know what version of Android you are testing this on? – Ragunath Jawahar Aug 08 '13 at 09:00
  • I've tested this on 4.2, 4.1, and 2.3. Other than 4.2, the error disappears when I press backspace – jramoyo Aug 08 '13 at 09:28
  • @jramoyo, thank you for helping out. So, the problem is with Android itself. – Ragunath Jawahar Aug 08 '13 at 11:36
2

You can use the default Android validation API.

Here is a very simple tutorial: http://blog.donnfelker.com/2011/11/23/android-validation-with-edittext/

The key is to use the setError method on your EditText. It will trigger default validation UI with provided error text.

Aerilys
  • 1,628
  • 1
  • 16
  • 22
1

for validation of edittext, use android:inputtype, android:maxLength.

Apart from this, can use regex for validation of form

Zoombie
  • 3,590
  • 5
  • 33
  • 40
0

You have two possibilities:

  • listen to changes to the field's content and run validation of that specific field or
  • listen to the submit-button click and validate the content of all fields on submit.

Else validation is just the same as in every other Java app: just test your constraints.

BTW: your question was already answered on stackoverflow.

Community
  • 1
  • 1
dst
  • 1,770
  • 13
  • 26
0

try this

    if(phone.getText().toString().isEmpty()){
        if(phone.lenth <= 10){
         
        }else{ // phone is`t correct }
        phone.setError("phone number is empty ");
        phone.requestFocus();
        return;
    }
    if(password.getText().toString().isEmpty()){
        if(password.lenth <= 6){
         
        }else{ // password is`t correct }
        password.setError("password number is empty ");
        password.requestFocus();
        return;
    }
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 21 '22 at 13:06