main.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"
>
<EditText
android:id="@+id/edtName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:ems="10"
android:hint="@string/Name"
/>
<EditText
android:id="@+id/edtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:ems="10"
android:hint="@string/EmailID"
android:inputType="textEmailAddress"
android:maxLength="49"
/>
<EditText
android:id="@+id/edtMobile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:ems="10"
android:hint="@string/MobileNumber"
android:inputType="phone"
android:maxLength="14"
/>
<Button
android:id="@+id/btnCheckValidation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/CheckValidation"
/>
</LinearLayout>
Validation.java
import java.util.regex.Pattern;
import android.text.TextUtils;
import android.widget.EditText;
public class Validation {
private static final
String EMAIL_MSG = "Invalid Email";
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,})$";
private static final
String REQUIRED_MSG = "This field is required";
private static final
String PHONE_REGEX = "^[0-9]{10}$";
private static final
String PHONE_MSG = "10 digit number required";
public static boolean isEmailAddress(EditText editText, boolean required) {
return isValid(editText, EMAIL_REGEX, EMAIL_MSG, required);
}
public static boolean isPhoneNumber(EditText editText, boolean required) {
return isValid(editText, PHONE_REGEX, PHONE_MSG, required);
}
public static boolean isValid(EditText editText, String regex,
String
errMsg, boolean required) {
String
text = editText.getText().toString().trim();
editText.setError(null);
if (required && !hasText(editText))
return false;
if (required && !Pattern.matches(regex,
text)) {
editText.setError(errMsg);
return false;
}
;
return true;
}
public static boolean hasText(EditText editText) {
String
text = editText.getText().toString().trim();
editText.setError(null);
if (text.length() == 0) {
editText.setError(REQUIRED_MSG);
return false;
}
return true;
}
public static boolean validateForNull(EditText p_editText, String p_nullMsg) {
boolean m_isValid = false;
try {
if (p_editText != null
&& p_nullMsg != null) {
if (TextUtils.isEmpty(p_editText.getText().toString().trim()))
{
p_editText.setError(p_nullMsg);
m_isValid
= false;
}
else {
m_isValid
= true;
}
}
}
catch (Throwable p_e) {
p_e.printStackTrace();
}
return m_isValid;
}
}
MainActivity.java
public class MainActivity extends Activity {
private EditText edtName, edtEmail, edtMobile;
private Button btnCheckValidation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edtName = (EditText) findViewById(R.id.edtName);
edtEmail =
(EditText) findViewById(R.id.edtEmail);
edtMobile = (EditText) findViewById(R.id.edtMobile);
btnCheckValidation = (Button) findViewById(R.id.btnCheckValidation);
btnCheckValidation.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (checkNullValidation()) {
if (checkValidation()) {
Toast.makeText(getApplicationContext(),
"True",
Toast.LENGTH_SHORT).show();
}
}
}
});
}
private boolean checkNullValidation() {
boolean isValid = true;
if (!Validation.validateForNull(edtName, "Name
is required")) {
isValid
= false;
}
if (!Validation.validateForNull(edtEmail, "Email
Id is required")) {
isValid
= false;
}
if (!Validation.validateForNull(edtMobile, "Mobile
No. is required")) {
isValid
= false;
}
return isValid;
}
private boolean checkValidation() {
boolean isValid = true;
if (!Validation.isEmailAddress(edtEmail, true))
isValid
= false;
if (!Validation.isPhoneNumber(edtMobile, true))
isValid
= false;
return isValid;
}
}
No comments:
Post a Comment