Meera436
Meera436

Reputation: 55

Input validation in Android

I am getting an error for the IsInputEditTextEmail boolean method. I know the matches parameter for Patterns.EMAIL_ADDRESS.matcher(value.matches()) is supposed to take in a parameter just unsure as to what the parameter should be?

Error Message

The attached image is the error I am receiving for the InputValidation.java code which is shown below.

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);

    }

Upvotes: 1

Views: 309

Answers (4)

androider
androider

Reputation: 140

you just have to take a global variable with some name like pattern match and after that you have to create an method and you can call that method anywhere where you have to validate the email. method will split the string and check whether it is matching the pattern or not if it is false it will generate error otherwise it will return the result true and based on that result you can do whatever your project need is. example is given below

       /*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;

Upvotes: 0

Amir
Amir

Reputation: 179

Patterns.EMAIL_ADDRESS.matcher(value.matches()) does not return boolean value. in order to return boolean value you should use matches() method like below in the if statement

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

Upvotes: 0

suresh madaparthi
suresh madaparthi

Reputation: 376

Create Util Class. Add isValidEmaillId Method in Util Class

         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();
}

use this ur Actitty class

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

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

            }

Upvotes: 0

Naveen Niraula
Naveen Niraula

Reputation: 771

You are getting an error because .matcher() takes CharSequence as argument but you are passing boolean because value.matches() returns boolean.

So instead of

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

You should be doing

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

Upvotes: 1

Related Questions