Showing posts with label Validation. Show all posts
Showing posts with label Validation. Show all posts

Tuesday, March 20, 2012

How to validate EmailId field in Android?


We use regular expressions along with Pattern & Matcher classes and their methods to validate email address

Required regular expression is  as below:

String regExpn = m:val="0"m:val="0"m:val="centerGroup"m:val="1440"m:val="subSup"m:val="undOvr"
"^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@"
    +"((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
      +"[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
      +"([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
      +"[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
      +"([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$";

Validation code goes as below:

private Boolean isValidEmail(String inputEmail)
{
      Pattern patternObj = Pattern.compile(regExpn);

      Matcher matcherObj = patternObj.matcher(inputEmail/*May be fetched from  EditText. This string is the one which you wanna validate for email*/);
            if (matcherObj.matches())
            {
                  //Valid email id…
                  return true;
}
            Else
{
//not a valid email id…
                  return false;
}
}


Happy coding:-)

Monday, February 13, 2012

How to validate all EditText in Android Layout?


private boolean validate() {
final int childrenCount = root.getChildCount();
for (int i = 0; i < childrenCount; i++) {
View child = root.getChildAt(i);
if (child instanceof EditText) {
if (TextUtils.isEmpty(((EditText) child).getText().toString()))
return false;
}
}
return true;
}

Sunday, February 5, 2012

How to validate the Password Field to be alphaNumeric in android?

You just need to pass the password string in following method.........


public boolean containAlphanumeric(final String str) {
byte counter = 0;
boolean checkdigit = false, checkchar = false;
for (int i = 0; i < str.length() && counter < 2; i++) {
   // If we find a non-digit character we return false.
   if (!checkdigit && Character.isDigit(str.charAt(i))) {
checkdigit = true;
counter++;
   }
   String a = String.valueOf(str.charAt(i));
   if (!checkchar && a.matches("[a-zA-Z]*")) {
checkchar = true;
counter++;
   }
}
if (checkdigit && checkchar) {
   return true;
}
return false;
    }