Today
we will learn about Android
Form Validation. In any app input validation is a very important thing to
do. Input Validation eliminates the errors that can be done by user while
giving inputs to our app. For example if we want to get the user’s email
we can check the entered email is a valid email or not before storing it inside
the database. So lets start our Android
Form Validation tutorial.
follow the source code of form validation in android example.
1.MainActivity.java:
package com.akash.checkvalidation;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText editTextfirstName,editTextlastname,editTextPassword,editTextMobile;
Button buttonSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextfirstName=(EditText)findViewById(R.id.editTextfirstName);
editTextlastname=(EditText)findViewById(R.id.editTextlastname);
editTextPassword=(EditText)findViewById(R.id.editTextPassword);
editTextMobile=(EditText)findViewById(R.id.editTextMobile);
buttonSubmit=(Button)findViewById(R.id.buttonSubmit);
buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
chekvalidation();
}
});
}
public void chekvalidation(){
if(editTextfirstName.getText().toString().equals("")){
Toast.makeText(this,"Please Enter First Name",Toast.LENGTH_SHORT).show();
}
if(editTextlastname.getText().toString().equals("")){
Toast.makeText(this,"Please Enter Last Name",Toast.LENGTH_SHORT).show();
}
if(editTextPassword.getText().toString().equals("")){
Toast.makeText(this,"Please Enter Password ",Toast.LENGTH_SHORT).show();
}
if(editTextMobile.getText().toString().equals("")){
Toast.makeText(this,"Please Enter Mobile Number",Toast.LENGTH_SHORT).show();
}
}
}
No comments:
Post a Comment