Tuesday 12 March 2013

ANDROID FORM VALIDATION EXAMPLE

Form Validation Example:

This is a Examle showing how to validat the data user entered
The form validation has done using the RegularExpressions 


1.Create New Android Projet


2.Add the following data in the main_activity.xml
res/layout/main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Username" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Password" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPassword" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Email" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textEmailAddress" />

    <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Submit" />

</LinearLayout>

3.Create The Activity Class For The xml File As Shown Below

package com.example.gurumurthy;

import java.util.regex.Pattern;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {
private static final Pattern EMAIL_PATTERN = Pattern
.compile("[a-zA-Z0-9+._%-+]{1,100}" + "@"
+ "[a-zA-Z0-9][a-zA-Z0-9-]{0,10}" + "(" + "."
+ "[a-zA-Z0-9][a-zA-Z0-9-]{0,20}"+
             ")+");
private static final Pattern USERNAME_PATTERN = Pattern
.compile("[a-zA-Z0-9]{1,250}");
private static final Pattern PASSWORD_PATTERN = Pattern
.compile("[a-zA-Z0-9+_.]{4,16}");
EditText usname;
EditText pword;
EditText email1;
Button btSubmit;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("HELLO","ONCREATE");
usname = (EditText) findViewById(R.id.editText1);
pword = (EditText) findViewById(R.id.editText2);
email1 = (EditText) findViewById(R.id.editText3);
btSubmit = (Button) findViewById(R.id.btnSubmit);

btSubmit.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
String username = usname.getText().toString();
String password = pword.getText().toString();
String email = email1.getText().toString();
if (username.equals("") || password.equals("")
|| email.equals("")) {
if (username.equals("")) {
Toast.makeText(MainActivity.this, "ENTER USERNAME",
Toast.LENGTH_LONG).show();

}
if (password.equals("")) {
Toast.makeText(MainActivity.this, "ENTER PASSWORD",
Toast.LENGTH_LONG).show();

}
if (email.equals("")) {
Toast.makeText(MainActivity.this, "ENTER EMAIL ID",
Toast.LENGTH_LONG).show();

}
} else {
if (!CheckUsername(username)) {
Toast.makeText(MainActivity.this, "ENTER VALID USERNAME",
Toast.LENGTH_LONG).show();
}
if (!CheckPassword(password)) {
Toast.makeText(MainActivity.this, "ENTER VALID PASSWORD",
Toast.LENGTH_LONG).show();
}
if (!CheckEmail(email)) {
Toast.makeText(MainActivity.this, "ENTER VALID EMAIL ID",
Toast.LENGTH_LONG).show();
}
}

}

private boolean CheckEmail(String email) {

return EMAIL_PATTERN.matcher(email).matches();
}

private boolean CheckPassword(String password) {

return PASSWORD_PATTERN.matcher(password).matches();
}

private boolean CheckUsername(String username) {

return USERNAME_PATTERN.matcher(username).matches();
}
});

}

}


4.After creating an Activity file Register the Activity File in the manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.gurumurthy"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.gurumurthy.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


COMMENT DON'T FORGET

5 comments:

  1. Good Tutorial Explained me clearly validations working fine thank for the help

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Very Useful this tutorial for Android form Validation.....

    ReplyDelete
  4. very helpful..... for validation

    ReplyDelete
  5. very helpful for me this tutorial for android form validation

    ReplyDelete