Last active
September 7, 2016 05:25
-
-
Save umanda/37b31636a7e064e33a9cd23136fc7791 to your computer and use it in GitHub Desktop.
Android Form Validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
android:padding="16dp"> | |
<EditText | |
android:id="@+id/edt_name" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:backgroundTint="@color/colorPrimaryDark" | |
android:hint="@string/enter_your_name" | |
android:inputType="textPersonName" /> | |
<EditText | |
android:id="@+id/edt_email" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:layout_marginBottom="10dp" | |
android:layout_marginTop="10dp" | |
android:backgroundTint="@color/colorPrimaryDark" | |
android:hint="@string/enter_your_email" | |
android:inputType="textEmailAddress" /> | |
<EditText | |
android:id="@+id/edt_message" | |
android:layout_width="fill_parent" | |
android:layout_height="150dp" | |
android:layout_marginBottom="10dp" | |
android:layout_marginTop="10dp" | |
android:backgroundTint="@color/colorPrimaryDark" | |
android:gravity="top" | |
android:hint="@string/enter_your_message" | |
android:fitsSystemWindows="true" | |
android:breakStrategy="balanced" | |
android:inputType="textMultiLine" | |
android:singleLine="false" | |
android:padding="5dp" /> | |
<Button | |
android:id="@+id/btn_send_form" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_horizontal" | |
android:background="@color/colorPrimaryDark" | |
android:elevation="4dp" | |
android:paddingLeft="70dp" | |
android:paddingRight="70dp" | |
android:text="@string/send" | |
android:textColor="#fff" /> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.umandajayobandara.app.util; | |
import android.widget.EditText; | |
import java.util.regex.Pattern; | |
public class FormValidation { | |
// Regular Expression | |
// you can change the expression based on your need | |
private static final String EMAIL_REGEX = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; | |
// Error Messages | |
private static final String REQUIRED_MSG = "required"; | |
private static final String EMAIL_MSG = "invalid email"; | |
// call this method when you need to check email validation | |
public static boolean isEmailAddress(EditText editText, boolean required) { | |
return isValid(editText, EMAIL_REGEX, EMAIL_MSG, required); | |
} | |
// call this method when you need to check phone number validation | |
public static boolean isMessage(EditText editText) { | |
return hasText(editText); | |
} | |
// return true if the input field is valid, based on the parameter passed | |
public static boolean isValid(EditText editText, String regex, String errMsg, boolean required) { | |
String text = editText.getText().toString().trim(); | |
// clearing the error, if it was previously set by some other values | |
editText.setError(null); | |
// text required and editText is blank, so return false | |
if ( required && !hasText(editText) ) return false; | |
// pattern doesn't match so returning false | |
if (required && !Pattern.matches(regex, text)) { | |
editText.setError(errMsg); | |
return false; | |
}; | |
return true; | |
} | |
// check the input field has any text or not | |
// return true if it contains text otherwise false | |
public static boolean hasText(EditText editText) { | |
String text = editText.getText().toString().trim(); | |
editText.setError(null); | |
// length 0 means there is no text | |
if (text.length() == 0) { | |
editText.setError(REQUIRED_MSG); | |
return false; | |
} | |
return true; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.umandajayobandara.app.activity; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.text.Editable; | |
import android.text.TextWatcher; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import android.widget.Toast; | |
import com.umandajayobandara.app.R; | |
import com.umandajayobandara.app.util.FormValidation; | |
public class SubmitForm extends AppCompatActivity { | |
private EditText editTextName; | |
private EditText editTextEmail; | |
private EditText editTextMessage; | |
private Button btnSubmit; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_submit_frorm); | |
submitContactUsForm(); | |
} | |
private void submitContactUsForm() { | |
editTextName = (EditText) findViewById(R.id.edt_name); | |
// TextWatcher would let us check validation error on the fly | |
editTextName.addTextChangedListener(new TextWatcher() { | |
public void afterTextChanged(Editable s) { | |
FormValidation.hasText(editTextName); | |
} | |
public void beforeTextChanged(CharSequence s, int start, int count, int after){} | |
public void onTextChanged(CharSequence s, int start, int before, int count){} | |
}); | |
editTextEmail = (EditText) findViewById(R.id.edt_email); | |
editTextEmail.addTextChangedListener(new TextWatcher() { | |
// after every change has been made to this editText, we would like to check validity | |
public void afterTextChanged(Editable s) { | |
FormValidation.isEmailAddress(editTextEmail, true); | |
} | |
public void beforeTextChanged(CharSequence s, int start, int count, int after){} | |
public void onTextChanged(CharSequence s, int start, int before, int count){} | |
}); | |
editTextMessage = (EditText) findViewById(R.id.edt_message); | |
editTextMessage.addTextChangedListener(new TextWatcher() { | |
public void afterTextChanged(Editable s) { | |
FormValidation.isMessage(editTextMessage); | |
} | |
public void beforeTextChanged(CharSequence s, int start, int count, int after){} | |
public void onTextChanged(CharSequence s, int start, int before, int count){} | |
}); | |
btnSubmit = (Button) findViewById(R.id.btn_send_form); | |
btnSubmit.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
/* | |
Validation class will check the error and display the error on respective fields | |
but it won't resist the form submission, so we need to check again before submit | |
*/ | |
if ( checkValidation () ) | |
submitForm(); | |
else | |
Toast.makeText(getApplicationContext(), "Form contains error", Toast.LENGTH_LONG).show(); | |
} | |
}); | |
} | |
private void submitForm() { | |
// Submit your form here. your form is valid | |
Toast.makeText(this, "Submitting form...", Toast.LENGTH_LONG).show(); | |
} | |
private boolean checkValidation() { | |
boolean ret = true; | |
if (!FormValidation.hasText(editTextName)) ret = false; | |
if (!FormValidation.isEmailAddress(editTextEmail, true)) ret = false; | |
if (!FormValidation.isMessage(editTextMessage)) ret = false; | |
return ret; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment