0

I am trying to validate the email add. which user enters in my form but it always gives "please enter valid email address" error even though i write true email address.where and what should i do correction in email pattern or in if condition in my code? help me.

Here is my java file:

IndividualuserFragment.java:

         public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

                View rootview = inflater.inflate(R.layout.fragment_individualuser, container, false);
                save = (Button) rootview.findViewById(R.id.save);
                cancel = (Button) rootview.findViewById(R.id.cancel);

                email = (EditText) rootview.findViewById(R.id.editemail);
                password = (EditText) rootview.findViewById(R.id.editpassword);

 final String emailPattern = "^[_A-Za-z0-9-]+(\\\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\\\.[A-Za-z0-9]+)*(\\\\.[A-Za-z]{2,})$";

     fm =getFragmentManager();
     cancel.setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        fm.beginTransaction().replace(R.id.content_frame,new LoginFragment()).commit();
                    }
                });
                save.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                          if(!(email.getText().toString().matches(emailPattern) && email.getText().toString().length() > 0)){
                            email.setError("please valid email address");
                            email.requestFocus();
                        }
    else if(!(password.getText().toString().length()>=6 && password.getText().toString().length()<=10)){
                            password.setError("please enter valid password");
                            password.requestFocus();
                        }else {

        //                AsyncTask obj = new AsyncDemo();
        //                obj.execute();
                            new myasyncDemo().execute();
                        }
                    }
                });


                return rootview;
            }
dev
  • 69
  • 8

2 Answers2

3

You can take advantage of existing method for email validation.

   private boolean isValidEmail(final String email) {
     return !TextUtils.isEmpty(email) 
                   && Patterns.EMAIL_ADDRESS.matcher(email).matches();
   } 
Rahul
  • 10,457
  • 4
  • 35
  • 55
1

try to replace email.getText().toString().length() > 0 and use TextUtils.isEmpty(email.getText().toString()).

You need to first check empty String condition and then match your email condition.

So your condition is look like below:

if(TextUtils.isEmpty(email.getText().toString()) || !Patterns.EMAIL_ADDRESS.matcher(email.getText().toString()).matches()
{
  email.setError("please valid email address");
  email.requestFocus();
}
pRaNaY
  • 24,642
  • 24
  • 96
  • 146