Android中的输入验证

时间:2018-11-13 02:33:06

标签: java android

我收到IsInputEditTextEmail布尔方法错误。我知道Patterns.EMAIL_ADDRESS.matcher(value.matches())的matchs参数应该接受一个参数,只是不确定该参数应该是什么?

Error Message

附件图像是我收到的InputValidation.java代码错误,如下所示。

package edu.spelman.spelfitscmail.spelfit.helper;

import android.app.Activity;
import android.content.Context;
import android.util.Patterns;
import android.view.WindowManager;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.support.design.widget.TextInputEditText;
import android.support.design.widget.TextInputLayout;

public class InputValidation {

    private Context context;

    public InputValidation(Context context) {
        this.context = context;
    }

    public boolean isinputEditTextFilled(TextInputEditText textInputEditText, TextInputLayout textInputLayout, String message) {

        String value = textInputEditText.getText().toString().trim();
        if (value.isEmpty()) {
            textInputLayout.setError(message);
            hideKeyboardFrom(textInputEditText);
            return false;
        } else{
            textInputLayout.setErrorEnabled(false);
        }
        return true;
    }

    public boolean isInputEditTextEmail(TextInputEditText textInputEditText, TextInputLayout textInputLayout, String message){
        String value = textInputEditText.getText().toString().trim();
        if (value.isEmpty() || Patterns.EMAIL_ADDRESS.matcher(value.matches())){
            textInputLayout.setError(message);
            hideKeyboardFrom(textInputEditText);
            return false;
        } else {
            textInputLayout.setErrorEnabled(false);
        }
        return true;
    }

    public boolean isInputEditTextMatches(TextInputEditText textInputEditText1, TextInputEditText textInputEditText2, TextInputLayout textInputLayout, String message){
        String value1 = textInputEditText1.getText().toString().trim();
        String value2 = textInputEditText2.getText().toString().trim();
        if (!value1.contentEquals(value2)){
            textInputLayout.setError(message);
            hideKeyboardFrom(textInputEditText2);
            return false;
        } else{
            textInputLayout.setErrorEnabled(false);
        }
        return true;
    }
    private void hideKeyboardFrom(View view){
        InputMethodManager imm =(InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

    }

4 个答案:

答案 0 :(得分:1)

由于.matcher()CharSequence作为参数,但由于boolean返回value.matches(),而传递了boolean,因此您收到错误消息。

所以不是

Patterns.EMAIL_ADDRESS.matcher(value.matches())

您应该做

Patterns.EMAIL_ADDRESS.matcher(value).matches()

答案 1 :(得分:0)

创建实用程序类。在Util类中添加isValidEmaillId方法

         public static boolean isValidEmaillId(String email){
         return Pattern.compile("^(([\\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})$").matcher(email).matches();
}

使用您的Actitty类

        String Email= textInputEditText2.getText().toString().trim();

             if (Email.isEmpty()|| !Util.isValidEmaillId(Email)){
                Toast.makeText(this, "Must Enter Valid Email ", 
                     Toast.LENGTH_SHORT).show();
                return;

            }

答案 2 :(得分:0)

Patterns.EMAIL_ADDRESS.matcher(value.matches())不返回布尔值。为了返回布尔值,您应该在if语句中使用如下所示的matchs()方法

   String value = textInputEditText.getText().toString().trim();
    if (value.isEmpty() || Patterns.EMAIL_ADDRESS.matcher(value).matches())

答案 3 :(得分:0)

您只需要使用一个具有诸如模式匹配之类名称的全局变量,然后您必须创建一个方法,然后可以在需要验证电子邮件的任何地方调用该方法。方法将拆分字符串并检查是否与模式匹配是否为假,否则将生成错误,否则将返回结果为true,并根据该结果可以执行项目所需的任何操作。例子在下面

       /*take this as golabl variable*/
        private String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+\\.+[a-z]+";   

      /*in some method i have to validate that the entered email is valid or not*/
       boolean result = validateEmail();

         validateEmail() 

            {
                 String email = textInputEditText.getText().toString().trim();

                    if (!email.isEmpty()) {
                        if (email.length() != 0) {
                            String data[] = cc.split(",");
                            for (int i = 0; i < data.length; i++) {
                                if (!email.matches(emailPattern)) {
                                    textInputEditText.setError("Invalid");
                        return false;
                                }
                            }
                        }
                    }
return true;