-4

I want to add a verification to my edit text:

  • check if the name is not a number
  • check if the tel is a number
  • check if the password and password2 are equals

How can I do this?

if (name.isEmpty() || name.length() < 3 ||  ............) {
     et_name.setError("enter a valid name");
     valid = false;
} else {
     et_nom.setError(null);
}
if (tel.isEmpty() || ..........) {
     et_tel.setError("enter a valid tel");
     valid = false;
} else {
     et_tel.setError(null);
}
if (password.isEmpty() || password2.isEmpty() || ..........) {
     et_password.setError("enter a valid password");
     valid = false;
} else {
     et_password.setError(null);
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
Paolo.
  • 27
  • 4

3 Answers3

2

Okey...

first :

for name you don't want allow user to enter numbers so what you can do is add two attributes to your name EditText.

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
android:inputType="text"

This will allow user to enter only alphabets and blank space. so no need for verification on that programmatically.

Second :

Same for the telephone number. you have to add android:inputType="number" to your telephone EditText. So user will able to input numbers only.

Third

for validating if your EditText is empty or not you can do following.

if (name.getText().toString().equals("")) {
    Toast.makeText(getApplicationContext(),
                    "Please enter your name", Toast.LENGTH_SHORT).show();
}

Fourth

For checking password equality you can do following.

if (!password1.getText().toString().equals(password2.getText().toString())) {
    Toast.makeText(getApplicationContext(),
                    "Please enter your name", Toast.LENGTH_SHORT).show();
}

Now Hopefully you understand this whole process. for your case refer following code.

if (name.getText().toString().equals("") || name.length() < 3 ||) {
        et_name.setError("enter a valid name");
        valid = false;
} 
if (tel.getText().toString().equals("")) {
        et_tel.setError("enter a valid tel");
        valid = false;
} 
if (password.getText().toString().equals("") || password2.getText().toString().equals("") || !password.getText().toString().equals(password2.getText().toString())) {
        et_password.setError("enter a valid password");
        valid = false;
}

Happy coding.

V-rund Puro-hit
  • 5,518
  • 9
  • 31
  • 50
1
  1. Why not just set the inputType="number" of the EditText in the xml? If you still prefer checking it dynamically, check out this post.

  2. TextUtils.equals() - http://developer.android.com/reference/android/text/TextUtils.html#equals(java.lang.CharSequence, java.lang.CharSequence)

Cheers! :D

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
0

Try using android.text.Utils.TextUtils class (part of android SDK)

See inputType

1. Check if name is number

if(TextUtils.isDigitsOnly(str)) {
  //number
}

2. Check if name is not a number

if(!TextUtils.isDigitsOnly(str)) {
  //number
}

3. Check if two password1 and password2 are equal

if(!TextUtils.isEmpty(password1) && !TextUtils.isEmpty(password2) && !password1.equals(password2)) {
  //number
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53