For example, I want to Validate Minimum-Length, and if it\'s Email
, a PhoneNumber
.
How is this possible in android. I wan
If you wan to prevent user to type something, then extend the InputFilter
and register it with your EditText
.
// built in InputFilter.LengthFilter limits the umber of chars
EditText.setFilters(new Filter[]{new InputFilter.LengthFilter(100)})
For the validation of Text box
1. minimum length: u can directly give the length of that text. 2. mail validation:
public boolean isEmailValid(String email)
{
String regExpn =
"^(([\\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})$";
CharSequence inputStr = email;
pattern = Pattern.compile(regExpn,Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(inputStr);
if(matcher.matches())
return true;
else
return false;
}
1. For phone number: If you want to fix length then give the length and the input type is number.
There are a number of things that you can do to validate
Add input filters. More on it is here http://developer.android.com/reference/android/text/InputFilter.html How to add filter to editable view is mentioned here http://developer.android.com/reference/android/text/Editable.html#setFilters%28android.text.InputFilter
Use TextWatchers to modify the content on the go. More on TextWatchers is here http://developer.android.com/reference/android/text/TextWatcher.html Set this up for your EditText using http://developer.android.com/reference/android/widget/TextView.html#addTextChangedListener(android.text.TextWatcher)
Note: There are few of them implemented in Android itself. Make use of them if you can. Look for subclasses in the documentation for TextWatcher and InputFilter
Also take a look here :) Android: Validate an email address (and more) in EditText