email validation image

Check mail id is valid or not in android

Check mail id is valid or not in android

Before sending mail need to check mail id is valid or not. Now in this tutorial we are going to learn how to validate mail id in android. We have taken here an Activity ‘EmailActivity.java’ and under a button click write the validation code.

Learn How to send mail in android.

ShortCode :-

public static boolean isEmailValid(String email) {

boolean isValid = false;

String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";

CharSequence inputStr = email;

Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);

Matcher matcher = pattern.matcher(inputStr);

if (matcher.matches()) {

isValid = true;

}

return isValid;

}

 

Source Code


 

 EmailActivit.java


 

package com.androidfromhome.emailvalidation;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EmailActivity extends Activity implements OnClickListener {

    private EditText et_mail;
    private Button btn_submit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_email);
        
        et_mail=(EditText)findViewById(R.id.et_email);
        btn_submit=(Button)findViewById(R.id.btn_submit);
        btn_submit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        
        switch (v.getId()) {
        case R.id.btn_submit:
            String mail=et_mail.getText().toString();
            if (!mail.equalsIgnoreCase("")) {
                
            
            if (isEmailValid(mail)) {
                Toast.makeText(EmailActivity.this,mail+" a valid mail id", Toast.LENGTH_LONG).show();
                
            }else {
                Toast.makeText(EmailActivity.this,mail+" is not valid mail id", Toast.LENGTH_LONG).show();
                
             }
            }else {
            
                
                Toast.makeText(EmailActivity.this,"Please Enter mail id", Toast.LENGTH_LONG).show();
                    
            }
            break;

        default:
            break;
        }
        
    }

    public static boolean isEmailValid(String email) {
        boolean isValid = false;
        String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
        CharSequence inputStr = email;
        Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }
}

 


activity_email.xml


 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#A758AB"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="15dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="2dp"
            android:text="Android From Home"
            android:textColor="#ffffff"
            android:textSize="22dp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical" >

        <EditText
            android:id="@+id/et_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="#dfdfdf"
            android:ems="10"
            android:hint="Enter Email"
            android:inputType="textEmailAddress"
            android:padding="10dp" />

        <Button
            android:id="@+id/btn_submit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#6FB35C"
            android:padding="10dp"
            android:text="Submit"
            android:textColor="#ffffff"
            android:textStyle="bold" />

    </LinearLayout>

</LinearLayout>

 


 

Download Full Source Code

Related Post:-

Image Download from server to android

Use Activity as dialog in android

How Retrieve contact list in android

ListView in android example with custom layout

Leave a Reply

Your email address will not be published. Required fields are marked *

Get Widget