2

For example, I want to Validate Minimum-Length, and if it's Email, a PhoneNumber.

How is this possible in android. I want to validate.

Matthew Bakaitis
  • 11,600
  • 7
  • 43
  • 53
Troj
  • 11,781
  • 12
  • 40
  • 49

4 Answers4

1

Also take a look here :) Android: Validate an email address (and more) in EditText

Andrea Baccega
  • 27,211
  • 13
  • 45
  • 46
1
  1. 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)})
    
Peter Knego
  • 79,991
  • 11
  • 123
  • 154
1

There are a number of things that you can do to validate

  1. 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

  2. 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

the100rabh
  • 4,077
  • 4
  • 32
  • 40
1

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.

Datta Kunde
  • 467
  • 6
  • 14