Thursday, 19 November 2015

How to validate PAN Card in android


       Here I am going to explain how to verify PAN card using regular expression in android devices

           


Step 1) MainActivity.java

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 MainActivity extends Activity {

EditText edittextPan;
Button button;
String Pan;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

edittextPan= (EditText) findViewById(R.id.edittext_pan);
button= (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() 
{

@Override
public void onClick(View v) 
{
PanedittextPan.getText().toString().trim();

Pattern pattern = Pattern.compile("[A-Z]{5}[0-9]{4}[A-Z]{1}");

Matcher matcher pattern .matcher(Pan);

if (matcher .matches()) {
Toast.makeText(getApplicationContext(), Pan+" is Matching",
Toast.LENGTH_LONG).show();

}
else
{
Toast.makeText(getApplicationContext(), Pan+" is Not Matching",
Toast.LENGTH_LONG).show();
}
}
});
   }
}


Step 2)activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:text="PAN Card Verification"
        android:textColor="@android:color/holo_green_dark"
        android:textStyle="bold|italic" />

    <EditText
        android:id="@+id/edittext_pan"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:inputType="textCapCharacters"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="50dp" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="50dp"
        android:layout_toRightOf="@+id/edittext_pan"
        android:background="@android:color/darker_gray"
        android:text="Verify" >
    </Button>

</RelativeLayout>