About
Home > Tutorials Home > Android > FormValidation in Android

FormValidation in Android

Published on October 17, 2019 8:23:29 PM

We are an online tutoring company that has laid its foundation in utilizing Multiple Intelligence methodology to understand specific needs and learning styles of a student.So that we patch a particular student to a tutor having a compatible teaching style. We have devised a smart online tutoring system to give you measurable results.Schools, cramped with students, fail to give your child the edge, the motivation and the right teacher guidance to help him find the true potential. We at Eonlinetutors.com make sure that your child gets the perfect learning environment to outperform.



Step 1 : Open the Eclipse and create a new Android project using Blank Activity application template. Give the application name "Form Validation in Android".

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal">

 	<LinearLayout
     	android:layout_width="fill_parent"
     	android:layout_height="wrap_content"
     	android:orientation="vertical">

  		<TextView
      		android:text="Form validations"
      		android:gravity="center"
      		android:layout_width="fill_parent"
      		android:layout_height="wrap_content"
      		android:textSize="20dp"
      		android:textColor="#ff00ff"/>

 		<LinearLayout
    		android:layout_width="fill_parent"
     	 	android:layout_height="wrap_content"
     	 	android:orientation="horizontal">
    
  		 	<TextView
      			android:text="Name"
      			android:layout_width="100dp"
      			android:layout_height="wrap_content"/>
  
  		 	<EditText
      			android:id="@+id/name"
      			android:layout_width="fill_parent"
	      		android:layout_height="wrap_content"
    	  		android:inputType="text"/>

  		</LinearLayout>

 		<LinearLayout
     	 	android:layout_width="fill_parent"
         	android:layout_height="wrap_content"
     	 	android:orientation="horizontal">

  		 	<TextView
       			android:text="Email"
      		 	android:layout_width="100dp"
        	 	android:layout_height="wrap_content" />
  
  			<EditText
      		 	android:id="@+id/email"
      		 	android:layout_width="fill_parent"
      		 	android:layout_height="wrap_content" />
  		</LinearLayout>
 
 		<LinearLayout
     		android:layout_width="fill_parent"
     		android:layout_height="wrap_content"
     		android:orientation="horizontal">

  			<TextView
      			android:text="Password"
      			android:layout_width="100dp"
      			android:layout_height="wrap_content" />

  			<EditText
      			android:id="@+id/password"
      			android:layout_width="fill_parent"
      			android:layout_height="wrap_content"
      			android:inputType="textPassword" />

  		</LinearLayout>
 
 		<LinearLayout
     		android:layout_width="fill_parent"
     		android:layout_height="wrap_content"
     		android:orientation="horizontal">
  
  			<TextView
      			android:text="Conform Password"
 	     		android:layout_width="100dp"
      			android:layout_height="wrap_content" />
  
  			<EditText
      			android:id="@+id/cpassword"
      			android:layout_width="fill_parent"
      			android:layout_height="wrap_content"
      			android:inputType="textPassword" />
  		</LinearLayout>
  
 		<LinearLayout
    	 	android:layout_width="fill_parent"
     	 	android:layout_height="wrap_content"
     	 	android:orientation="horizontal">

  			<TextView
      			android:text="Mobile"
      			android:layout_width="100dp"
      			android:layout_height="wrap_content" />
  		 
  			<EditText
      			android:id="@+id/mobile"
      			android:layout_width="fill_parent"
      			android:layout_height="wrap_content"
      			android:inputType="number"/>
  		 
  		</LinearLayout>
  	
  		<LinearLayout
    	 	android:layout_width="fill_parent"
     	 	android:layout_height="wrap_content"
     	 	android:orientation="horizontal">
  
			<TextView
				android:text="Birthday"
				android:layout_width="100dp"
				android:layout_height="wrap_content" />
	   
			<EditText
				android:id="@+id/birthday"
				android:layout_width="fill_parent"
				android:layout_height="wrap_content"
				android:inputType="date"
				android:hint="dd/mm/yyyy" />
		 
   		</LinearLayout>
  
 		<LinearLayout
    	 	android:layout_width="fill_parent"
     	 	android:layout_height="wrap_content"
         	android:orientation="horizontal">
  			
			<TextView
    	 		android:text="Message"
      	  		android:layout_width="100dp"
      	  		android:layout_height="wrap_content" />
   
			<EditText
				android:id="@+id/message"
				android:layout_width="fill_parent"
				android:layout_height="wrap_content" />
  		
		</LinearLayout>
  
 		<LinearLayout
    	 	android:layout_width="fill_parent"
     	 	android:layout_height="wrap_content"
     	 	android:orientation="horizontal">
 
  			<Button
    	  		android:id="@+id/submit"
      	  		android:text="Submit"
      	  		android:layout_width="150dp"
          		android:layout_height="wrap_content" />

 			<Button
      			android:text="Cancel"
      			android:layout_width="150dp"
      			android:layout_height="wrap_content"  />
 		</LinearLayout>
 
  	</LinearLayout>
</LinearLayout>

Step 2 : Create another .xml file to your Project.

<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:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="U R Successfully Register..." />

     <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:text="Wel Come Guest..." />

</LinearLayout>

Step 3 : Copy the Below Code into your mainActivity.java File.

package com.example.validationexample;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

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

        final String str="[a-zA-Z0-9._-][email protected][a-z]+.[a-z]+";
        final String date="(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)";
        
        final EditText name=(EditText) findViewById(R.id.name);
        final EditText email=(EditText) findViewById(R.id.email);
        final EditText mobile=(EditText) findViewById(R.id.mobile);
        final EditText message=(EditText) findViewById(R.id.message);
        final EditText birthday=(EditText) findViewById(R.id.birthday);
        final EditText password=(EditText) findViewById(R.id.password);
        final EditText cpassword=(EditText) findViewById(R.id.cpassword);
        
        Button submit = (Button) findViewById(R.id.submit);
        
        submit.setOnClickListener(new OnClickListener()
        {
			public void onClick(View v)
			{
				if(name.getText().toString().length()==0)
		        { 
		        	name.setError( "Please Enter Name" );
		        }
				else if(email.getText().toString().length()==0)
		        { 
		        	email.setError( "Please Enter Email" );
		        }
				else if(email.getText().toString().matches(str)==false)
				{
					email.setError("Please Enter Valid Email Id");
				}
				else if(password.getText().toString().length()==0)
				{
					password.setError("Please Enter Password");
				}
				else if(cpassword.getText().toString().length()==0)
				{
					cpassword.setError("Entern Conform Password");
				}
				else if(cpassword.getText().toString().matches(password.getText().toString())==false)
				{
					cpassword.setError("Password Not Match");
				}
				else if(mobile.getText().toString().length()==0)
		        {
		        	mobile.setError( "Please Enter Mobile" );
		        }
				else if(mobile.getText().toString().length()!=10)
				{
					mobile.setError("Enter 10 digits only");
				}
				else if(birthday.getText().toString().length()==0)
				{
					birthday.setError("Enter Birthday");
				}
				else if(birthday.getText().toString().matches(date)==false)
				{
					birthday.setError("Enter Valid Birthday");
				}
				else if(message.getText().toString().length()==0)
		        {
		        	message.setError( "Please Enter Message" );
		        }
				else
				{
					Intent ii=new Intent(MainActivity.this,Home.class);
					startActivity(ii);
				}
			}
		});
    }
}
ALL COMMENTS (2)
Abhishek Bedre 2017-03-28 17:25:15
I have implemented my own "jarvis"....thanks to this example.        
2019-01-03 13:22:19
It is working fine in desktop but not working in android mobile