Showing posts with label email validation. Show all posts
Showing posts with label email validation. Show all posts

Sunday, 28 April 2013

Name, Phone Number, Email Validation using Saripaar Example in Android

Download Saripaar Library from gitHub 


Add Saripaar Library Project to your project, (same method of adding actionbarsherlock to your project)

main.xml


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

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="18dp"
            android:layout_marginTop="30dp"
            android:text="Name"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/name_add_edit_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignTop="@+id/textView1"
            android:ems="10" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView1"
            android:layout_below="@id/name_add_edit_text"
            android:layout_marginTop="60dp"
            android:text="IpAddress"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <EditText
            android:id="@+id/ipaddress_edit_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView2"
            android:layout_alignBottom="@+id/textView2"
            android:layout_toRightOf="@+id/textView2"
            android:ems="10" />

        <CheckBox
            android:id="@+id/terms_conditions_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/textView2"
            android:layout_below="@id/ipaddress_edit_text"
            android:layout_marginTop="56dp"
            android:text="I accept terms and conditions" />

        <Button
            android:id="@+id/add_edit_done_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/terms_conditions_checkbox"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="27dp"
            android:text="SAVE" />

    </RelativeLayout>

PhoneNumberEmailValidationActivity.java

package com.rajeshvijayakumar.saripaar;

import com.mobsandgeeks.saripaar.Rule;
import com.mobsandgeeks.saripaar.Validator;
import com.mobsandgeeks.saripaar.Validator.ValidationListener;
import com.mobsandgeeks.saripaar.annotation.Email;
import com.mobsandgeeks.saripaar.annotation.NumberRule;
import com.mobsandgeeks.saripaar.annotation.NumberRule.NumberType;
import com.mobsandgeeks.saripaar.annotation.Required;
import com.mobsandgeeks.saripaar.annotation.TextRule;

public class PhoneNumberEmailValidationActivity extends Activity implements ValidationListener {

    @Required(order = 1)
    @TextRule(order = 2, minLength = 5, maxLength = 35, trim = true, message = "Enter Valid Full Name")
    private EditText mNameEditText;

    @Required(order = 3)
    @Email(order = 4, message = "Please Check and Enter a valid Email Address")
    private EditText mEmailEditText;

    @Required(order = 5)
    @NumberRule(order = 6, message = "Enter Phone Number in Numeric", type = NumberType.LONG)
    @TextRule(order = 7, message = "Enter valid Phone Number", minLength = 10, maxLength = 14)
    private EditText mPhoneNumberEditText;

    private Button mAddEditButton;
    private Validator mAddEditValidator;

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

        mNameEditText = (EditText) findViewById(R.id.name);
        mEmailEditText = (EditText) findViewById(R.id.email_address);
        mPhoneNumberEditText = (EditText) findViewById(R.id.phone_number);
        mAddEditButton = (Button) findViewById(R.id.validate_button);

        mAddEditButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                ((com.mobsandgeeks.saripaar.Validator) mAddEditValidator).validate();
            }
        });
        mAddEditValidator = new Validator(this);
        mAddEditValidator.setValidationListener(this);

    }

    @Override
    public void onFailure(View failedView, Rule<?> failedRule) {

        String message = failedRule.getFailureMessage();
        if (failedView instanceof EditText) {
            Toast.makeText(this, message,
                    Toast.LENGTH_SHORT).show();
        }  else {
            Toast.makeText(this, "Record Not Saved", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onSuccess() {
        Toast.makeText(this, "Record Saved Successfully", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onValidationCancelled() {
    }

    @Override
    public void preValidation() {
    }
}


Output: 






Source Code :  Download Example with Library