
Hii Developers in this article you can read how to use validation in android. Hare is an easy way to implement Email and phone number validation in Android. Also, you can see an empty field and match string like password and confirm password.
Let’ s Start on firstly make a registration form and create a file. after the get string from input filed and check them on button click filed are empty and not. and also create a format like email using @. com, And Mobile no Lenth and formate
Email and phone number validation in Android
Android validation for email included with formate and input type.
String EMAIL_STRING = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
+ "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
XML Code for Email Validation
<EditText android:id="@+id/RegEmail" android:layout_width="match_parent" android:layout_height="50dp" android:hint="Enter email address" android:inputType="textEmailAddress" android:singleLine="true" android:layout_weight="1"/>
Java Code for Email Validation
public class Registration extends AppCompatActivity implements View.OnClickListener { EditText RegEmail; Button RegButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_registration); RegEmail=(EditText) findViewById(R.id.RegEmail); RegButton=(Button)findViewById(R.id.RegButton); RegButton.setOnClickListener(this); }
@Override public void onClick(View v) { String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+"; if(Reg_Name.equals("") || Reg_Name.equals(null)) { RegName.setError("Username can't be empty"); return; } else if (Reg_Email.equals("") || Reg_Email.equals(null)||!Reg_Email.matches(emailPattern)) { RegEmail.setError("Please enter right email Address"); return; } else{ } } }
Android Mobile No Validation.
hare I am sharing how to used mobile no validation in android. including length and fix input limited text in the input box. inputType number only. and add test only singleLine , mobile no maxLegnth
<EditText android:id="@+id/RegMobile" android:layout_width="match_parent" android:layout_height="50dp" android:hint="Enter your phone number" android:inputType="number" android:singleLine="true" android:maxLength="10" android:layout_weight="1"/>
Java code for phone number validation
String pattern = "^\\s*(?:\\+?(\\d{1,3}))?[-. (]*(\\d{3})[-. )]*(\\d{3})[-. ]*(\\d{4})(?: *x(\\d+))?\\s*$";
else if (Reg_Mobile.equals("") || Reg_Mobile.equals(null)||Reg_Mobile.length()<10) { RegMobile.setError("Enter a right mobile number"); return;
Android Password and confirm password Validation.
<EditText android:id="@+id/RegPaswword" android:layout_width="match_parent" android:layout_height="50dp" android:hint="Enter your password" android:singleLine="true" android:inputType="textPassword" android:layout_weight="1"/>
else if(!Reg_Paswword.equals(Reg_ConformPassword)){ Toast.makeText(Registration.this,"Password Not matching",Toast.LENGTH_LONG).show(); RegConformPassword.setError("Password Not matching"); return; }