Input validation in Android












1















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

}









share|improve this question





























    1















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

    }









    share|improve this question



























      1












      1








      1








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

      }









      share|improve this question
















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

      }






      java android






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 6:08









      Shashanth

      2,49642136




      2,49642136










      asked Nov 13 '18 at 2:33









      Meera436Meera436

      204




      204
























          4 Answers
          4






          active

          oldest

          votes


















          0














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





          share|improve this answer































            1














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





            share|improve this answer































              0














              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;

              }





              share|improve this answer

































                0














                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;





                share|improve this answer























                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272938%2finput-validation-in-android%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  0














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





                  share|improve this answer




























                    0














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





                    share|improve this answer


























                      0












                      0








                      0







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





                      share|improve this answer













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






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 13 '18 at 6:44









                      AmirAmir

                      159214




                      159214

























                          1














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





                          share|improve this answer




























                            1














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





                            share|improve this answer


























                              1












                              1








                              1







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





                              share|improve this answer













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






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 13 '18 at 3:44









                              Naveen NiraulaNaveen Niraula

                              347214




                              347214























                                  0














                                  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;

                                  }





                                  share|improve this answer






























                                    0














                                    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;

                                    }





                                    share|improve this answer




























                                      0












                                      0








                                      0







                                      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;

                                      }





                                      share|improve this answer















                                      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;

                                      }






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 13 '18 at 4:35

























                                      answered Nov 13 '18 at 4:29









                                      suresh madaparthisuresh madaparthi

                                      25419




                                      25419























                                          0














                                          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;





                                          share|improve this answer




























                                            0














                                            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;





                                            share|improve this answer


























                                              0












                                              0








                                              0







                                              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;





                                              share|improve this answer













                                              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;






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 13 '18 at 6:44









                                              Anusha MathurAnusha Mathur

                                              7513




                                              7513






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53272938%2finput-validation-in-android%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Florida Star v. B. J. F.

                                                  Danny Elfman

                                                  Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues