0

I'm completely new to android and development too. I created a page to take the email EditText, password EditText and signup button. So here how can I link this EditText to code to verify the entered values in both EditText is valid?

Below is the code that i'm trying to use.

  public void isEmailValid(View view) {
    this.view = view;
    EditText editText = (EditText) findViewById(R.id.editText);
    if (editText.getText().toString().matches("[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+") && editText.length() > 0) {
        editText.setText("valid email");
    } else {
        editText.setText("invalid email");
    }
}

Thanks in advance.

kick07
  • 614
  • 1
  • 8
  • 19

3 Answers3

0

If you want to evaluate email address after clicking the button you might set a click listener for your button and do it :

Button yourButton = (Button) findViewById(R.id.your_button);
yourButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            evaluateEmail();
        }
});

and here is your method for evaluation :

private void evaluateEmail() {
    EditText editText = (EditText) findViewById(R.id.editText);
    if (editText.getText().toString().matches("[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+") && editText.length() > 0) {
       //it is valid
    } else {
        //it is not valid
    }
}
Meikiem
  • 1,876
  • 2
  • 11
  • 19
0

Try this code:

 public final static boolean isValidEmail(CharSequence target) {
   return !TextUtils.isEmpty(target) && 
      android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
   }

How can we perform Email Validation on edittext in android ? I have gone through google & SO but I didn't find out a simple way to validate it. follow link..

How should I validate an e-mail address?

Sarvesh Verma
  • 116
  • 1
  • 6
0

You can use Android Patterns class to perform match for your email regex.

Patterns.EMAIL_ADDRESS.matcher(editText.getText().toString()).matches();
Paras
  • 3,197
  • 2
  • 20
  • 30