Hi!
Today i am going to show How to validate e-mail id & mobile number in android. Because some struggle with validation as specially for e-mail..,
Validation of email id & Mobile number in android is similar to validation in java.
You can Download my jar at Here and can add into your project
or
package ra_jEmail_Mob_Validation;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RValidation {
String r_email, r_mobil_no;
boolean is_mob_number = false, is_email = false;
//this is the default format for email validation
String regEx = "[a-zA-Z0-9+._%-+]{1,256}" + "@"
+ "[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" + "(" + "."
+ "[a-zA-Z0-9][a-zA-Z0-9-]{0,25}" + ")+";
// empty constructor for call the object
public RValidation() {
super();
}
// create getter & setter for parameters
public String getR_email() {
return r_email;
}
public void setR_email(String raj_email) {
this.r_email = raj_email;
}
public String getR_mobil_no() {
return r_mobil_no;
}
public void setR_mobil_no(String raj_mobil_no) {
this.r_mobil_no = raj_mobil_no;
}
//method to validate you mobile number, if it is valid it will return true
public boolean isIs_mob_number() {
// mobile number should be 10 digit
Pattern pattern = Pattern.compile("\\d{10}");
Matcher matchr = pattern.matcher(this.getR_mobil_no().trim());
if (matchr.matches()) {
is_mob_number = true;
}
return is_mob_number;
}
// method to validate you e-mail id, if it is valid it will return true
public boolean isIs_email() {
Matcher matcherObj = Pattern.compile(regEx).matcher(
this.getR_email().trim());
if (matcherObj.matches()) {
is_email = true;
}
return is_email;
}
}
Today i am going to show How to validate e-mail id & mobile number in android. Because some struggle with validation as specially for e-mail..,
Validation of email id & Mobile number in android is similar to validation in java.
You can Download my jar at Here and can add into your project
or
- create a bean class to forward your input to validate
package ra_jEmail_Mob_Validation;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RValidation {
String r_email, r_mobil_no;
boolean is_mob_number = false, is_email = false;
//this is the default format for email validation
String regEx = "[a-zA-Z0-9+._%-+]{1,256}" + "@"
+ "[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" + "(" + "."
+ "[a-zA-Z0-9][a-zA-Z0-9-]{0,25}" + ")+";
// empty constructor for call the object
public RValidation() {
super();
}
// create getter & setter for parameters
public String getR_email() {
return r_email;
}
public void setR_email(String raj_email) {
this.r_email = raj_email;
}
public String getR_mobil_no() {
return r_mobil_no;
}
public void setR_mobil_no(String raj_mobil_no) {
this.r_mobil_no = raj_mobil_no;
}
//method to validate you mobile number, if it is valid it will return true
public boolean isIs_mob_number() {
// mobile number should be 10 digit
Pattern pattern = Pattern.compile("\\d{10}");
Matcher matchr = pattern.matcher(this.getR_mobil_no().trim());
if (matchr.matches()) {
is_mob_number = true;
}
return is_mob_number;
}
// method to validate you e-mail id, if it is valid it will return true
public boolean isIs_email() {
Matcher matcherObj = Pattern.compile(regEx).matcher(
this.getR_email().trim());
if (matcherObj.matches()) {
is_email = true;
}
return is_email;
}
}
- Here Pass the value for validation from your Action class by on click listener
public class Register extends Activity {
EditText et_r_name,et_r_mob,et_r_email,r_pass;
//create reference for our class
RValidation r_valid;
boolean sucess_regist=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Button reg=(Button) findViewById(R.id.r_regist);
et_r_name=(EditText) findViewById(R.id.r_name);
et_r_mob=(EditText) findViewById(R.id.r_mob);
et_r_email=(EditText) findViewById(R.id.r_email);
r_pass=(EditText) findViewById(R.id.r_pass);
// create a new object for our reference
r_valid=new RValidation();
reg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!et_r_name.getText().toString().equals(""))
{
// pass the mobile number to through our setter methid
r_valid.setR_mobil_no(et_r_mob.getText().toString());
// if it is true move to next step
if(r_valid.isIs_mob_number())
{
//pass the e-mile id to through our setter methid
r_valid.setR_email(et_r_email.getText().toString());
//if it is true move to next step
if(r_valid.isIs_email())
{
if(!r_pass.getText().toString().equals(""))
{
AlertDialog.Builder ad= new AlertDialog.Builder(new ContextThemeWrapper(Register.this, R.style.popup_theme));
ad.setTitle("Success");
ad.setMessage("You Have Login Successfully..,");
ad.setInverseBackgroundForced(false);
ad.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
a ad.show();
}
else
{
r_pass.setError(Html.fromHtml("<font color='green'>Enter Your Password</font>"));
}
}
else
{
et_r_email.setError(Html.fromHtml("<font color='green'>Enter Your Valid E-mail address</font>"));
}
}
else
{
et_r_mob.setError(Html.fromHtml("<font color='green'>Mobile Number Should Be 10 Digit</font>"));
}
}
else
{
et_r_name.setError(Html.fromHtml("<font color='green'>Enter Your Name</font>"));
//
}
}
});
}
}
finally i have set some error messages and add some html tag for my customization..,
Thank You..,
Please Leave Your Comment..,
Have A Happy Day..,
sss
ReplyDelete