Validations In Android


Validation on user input can be applied by regular expressions. It provides a concise and flexible means for matching strings of text, such as particular characters, words, or patterns of characters.


  

Step:1  Designing xml as follows..

      


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
<TextView 
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:text="@string/hello"/>
<EditText android:id="@+id/editText1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:hint="Enter Email Address">
     </EditText>
<EditText android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:id="@+id/editText2"
     android:inputType="textPassword"
     android:hint="Password">
     </EditText>
<EditText android:layout_height="wrap_content"
     android:inputType="textPassword"
     android:layout_width="match_parent"
     android:id="@+id/editText3"
     android:hint="ConformPassword">
     </EditText>
<Button android:id="@+id/button1"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:text="CheckValidation"></Button>
</LinearLayout>

Java Code:

package com.murali.validation;

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 EmailValidationActivity extends Activity {
    /** Called when the activity is first created. */
      public EditText txt;
      public EditText pwd_txt;
      public EditText cpwd_txt;
      public Button check;
      public final Pattern EMAIL_ADDS_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);
     setContentView(R.layout.main);
       
     txt=(EditText) findViewById(R.id.editText1);

     pwd_txt=(EditText) findViewById(R.id.editText2);
     cpwd_txt=(EditText) findViewById(R.id.editText3);
           
     check=(Button) findViewById(R.id.button1);
     txt.setText("murali@gmail.com");
 
    check.setOnClickListener(new OnClickListener() {
     @Override
     public void onClick(View v) {
       // TODO Auto-generated method stub
                 
     String email=txt.getText().toString();

     String pwd=pwd_txt.getText().toString();
     String cpwd=cpwd_txt.getText().toString(); 
           
    if(CheckEmail(email))
       {
         Toast.makeText(EmailValidationActivity.this,"Valid Email Addresss", 1).show();
         //.....................................       
         if(cpwd.equalsIgnoreCase(pwd))// It is check the condition
                      Toast.makeText(EmailValidationActivity.this,"Password Matched",1).show();
             else
                cpwd_txt.setError("Not Match");
          //.....................................
          }
      else
          {   
            txt.setError("Invalid Email Addresss");
            Toast.makeText(EmailValidationActivity.this,"Invalid Email Addresss", 1).show();
           }
         }
    private boolean CheckEmail(String email) {
    // TODO Auto-generated method stub
      return  EMAIL_ADDS_PATTERN.matcher(email).matches();
              }
         });
     }

Comments

  1. Hi.,

    Very easy example to understand especially for beginners. Simply just great job...

    Regards
    Athar H.
    www.e-weddingcardswala.in

    ReplyDelete

Post a Comment

Popular posts from this blog

How To Run Compatibility Test Suite (CTS) ?

NFC : Reading a MiFare Classic 1K from Android Devices

Dalvik Virtual machine VS Java Virtual Machine: