Email validation in ANDROID.

By | May 1, 2011

This is a simple example showing email validation in ANDROID.
This example uses regex for email validation.
Create a button and an edittext in your main.xml file and try this code.

package com.coderzheaven;

import java.util.regex.Pattern;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class EmailValidationDemo extends Activity   {

	EditText TF;
	public Button checkButton;

	public final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile(
	          "[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}" +
	          ")+"
	      );
     @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);

	   TF=(EditText) findViewById(R.id.TF);
	   checkButton=(Button) findViewById(R.id.checkButton);

	    checkButton.setOnClickListener(new OnClickListener() {
		public void onClick(View v) {
			   String email=TF.getText().toString();
			   if(checkEmail(email))
				  Toast.makeText(EmailValidationDemo.this,"Valid Email Addresss", Toast.LENGTH_SHORT).show();
			   else
			 	  Toast.makeText(EmailValidationDemo.this,"Invalid Email Addresss", Toast.LENGTH_SHORT).show();
		}
	    });
    }
    private boolean checkEmail(String email) {
        return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
    }
}

Check this link to find how you can send email from android programatically.
Please leave your valuable comments……

10 thoughts on “Email validation in ANDROID.

  1. Farhan Mehmood

    yeah! worked like a chram! (^_^) thank u so much!

    Reply
  2. SATRASALA VINIL

    Thank u so much.. but can u make it to complete file please,, so that the user can know about the exceptions in manifest file also

    Reply
  3. Shreya

    Hi,
    I’m developing an android app where I’m storing encrypted password(md5) in database using php script. I can successfully insert encrypted password it in database but cannot check it with the value I’m entering in login form. So can you please tell me how to encrypt password in android?

    Reply
    1. James Post author

      Hello Shreya, You can encrypt the password using the “Cipher” class in android.

      Reply
  4. TechnoTalkative

    Hello,

    I have implemented the example and tested it, it was working fine in most cases, but when i tried to test abc.xyz@gmail , then it is showing “Valid Email Addresss”. I have not entered .com/.org/.co or any else still it is showing its valid.

    Reply
    1. James Post author

      ok TechnoTalkative..
      We will rectify it.
      Thanks for the information.

      Reply
    1. James Post author

      OK Ranjtih we will check that. most probably we might have missed that combination.

      Reply
  5. nami

    ya in my came whatever m typing with @gamil.com its showing valid…otherwise invalid

    Reply

Leave a Reply

Your email address will not be published.