Android: are there any good solutions how to validate Editboxes

前端 未结 4 1545
感动是毒
感动是毒 2021-01-07 04:01

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

How is this possible in android. I wan

相关标签:
4条回答
  • 2021-01-07 04:25
    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)})
      
    0 讨论(0)
  • 2021-01-07 04:33

    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.

    0 讨论(0)
  • 2021-01-07 04:34

    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

    0 讨论(0)
  • 2021-01-07 04:47

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

    0 讨论(0)
提交回复
热议问题