2

I wrote following code for login but when I type "\" after email-id, it accepts and log in successfully (it doesn't accepts any other symbols or characters only accepts "\").

I don't want that it login with "\".`

@Override
public void onCreate(Bundle savedInstanceState) {
    try {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
        setContentView(R.layout.main);
        WiztangoBaseApplication.InitDialogBox(WiztangoActivity.this);

        this.pref = PreferenceManager.getDefaultSharedPreferences(this);
        loginStatus = (TextView)findViewById(R.id.login_status);
        register = (Button) findViewById(R.id.btnregister);
        register.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                RegisterIntent();
            }
        });

        login = (Button) findViewById(R.id.btlogin);
        password = (EditText) findViewById(R.id.txtPwd);
        username = (EditText) findViewById(R.id.txtemail);
        saveLoginCheckBox = (CheckBox)findViewById(R.id.saveLoginCheckBox);
        pref = getSharedPreferences(Constants.PREFS_NAME,
                MODE_PRIVATE);
        String usernamestr = pref.getString(Constants.PREF_USERNAME, "");
        String passwordsharestr = pref.getString(Constants.PREF_PASSWORD,
        "");
        username.setText(usernamestr);
        password.setText(passwordsharestr);
        saveLogin = pref.getBoolean("saveLogin", false);
        if (saveLogin == true) {
            username.setText(usernamestr);
            password.setText(passwordsharestr);
            saveLoginCheckBox.setChecked(true);
        }

        login.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                try {
                    //Constants.clearInfo();
                    getSharedPreferences(Constants.PREFS_NAME, MODE_PRIVATE)
                    .edit()
                    .putString(Constants.PREF_USERNAME,
                            username.getText().toString())
                            .putString(Constants.PREF_PASSWORD,
                                    password.getText().toString())
                                    .putString(Constants.PREF_CHECKED, "TRUE")
                                    .commit();


                    if (username.getText().toString().trim().equals("")) {
                        username.setError(Html
                                .fromHtml("Please Enter Username"));
                        username.requestFocus();
                    } else if (password.getText().toString().trim()
                            .equals("")) {
                        password.setError(Html
                                .fromHtml("Please Enter Password"));
                        password.requestFocus();
                    } else {
                        if (Constants
                                .checkInternetConnection(WiztangoActivity.this)) {
                            Constants.userpass = password.getText()
                            .toString();
                            new AuthenticData().execute();
                        } else {
                            WiztangoBaseApplication.ShowThisDialog("Error",
                            "Please check internet connection.");
                        }

                        if (saveLoginCheckBox.isChecked()) {

                            prefEditor.putBoolean("saveLogin", true);
                            prefEditor.putString(Constants.PREF_USERNAME,
                                    username.getText().toString());
                            prefEditor.putString(Constants.PREF_PASSWORD,
                                    password.getText().toString());
                            saveLoginCheckBox.setChecked(true);
                            prefEditor.commit();
                        } else {

                            SharedPreferences mPreferences = getSharedPreferences(Constants.PREFS_NAME, MODE_PRIVATE); 
                            SharedPreferences.Editor editor=mPreferences.edit();

                            editor.remove(Constants.PREF_USERNAME);
                            editor.remove(Constants.PREF_PASSWORD);
                            editor.commit();


                        }
                    }

                } catch (Exception e) {
                    Log.e("Exception In Wiztango/", e.toString());
                    e.printStackTrace();
                }
            }
        });
    } catch (Exception e) {
        Log.e("Exception In Wiztango/", e.toString());
        e.printStackTrace();
    }
}

Charles
  • 50,943
  • 13
  • 104
  • 142
Varsha B.
  • 41
  • 3
  • 7

7 Answers7

4

Inbuilt Patterns Provides Email Validation like:

if (!android.util.Patterns.EMAIL_ADDRESS.matcher(emailStr).matches() && !TextUtils.isEmpty(emailStr)) {
    emailEditText.setError("Invalid Email");
    emailEditText.requestFocus();
}
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
1

Heyy, check this answer here: https://stackoverflow.com/a/7882950/1739882

It says:

public final static boolean isValidEmail(CharSequence target) {
    if (target == null) {
        return false;
    } else {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
    }
}
Community
  • 1
  • 1
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174
0

You can use this code for check is Valid Email or not:

public final static boolean isValidEmail(CharSequence target) {
     if (target == null) {
         return false;
     } else {
         return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
     }
}
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Dilip
  • 2,271
  • 5
  • 32
  • 52
0

Best Approach

Validate email and isEmpty in one method.

 public final static boolean isValidEmail(CharSequence target)
 {
     return !TextUtils.isEmpty(target) && android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
 }
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
-1
public static boolean isEmailValid(String email) {
        boolean isValid = false;

        String expression = "^[\\w\\.-]+@([\\w\\-]+\\.)+[A-Z]{2,4}$";
        CharSequence inputStr = email;

        Pattern pattern = Pattern.compile(expression, Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }
Nitin Misra
  • 4,472
  • 3
  • 34
  • 52
  • -1, don't use simple regexes to validate email addresses. That regex will not validate perfectly valid email addresses, including, say, the `.museum` TLD. – Charles Jan 02 '14 at 20:51
-1

Try this code:--

 public final static boolean isValidEmail(CharSequence target) {

 if (target == null) {
     return false;
 } else {
     return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
 }
}
Kailash Dabhi
  • 3,473
  • 1
  • 29
  • 47
  • -1, don't use simple regexes to validate email addresses. That regex does not comply with the various RFCs, and will fail by allowing excessively long addresses. The validation of the local part is also bogus, the RFCs permit a *lot* more punctuation there. – Charles Jan 02 '14 at 20:54
-1

Try out the below method to validate the Email address.

private boolean validateEmail(EditText editText, String p_nullMsg, String p_invalidMsg)
{
    boolean m_isValid = false;
    try
    {
        if (p_editText != null)
        {
            if(validateForNull(editText,p_nullMsg))
            {
                Pattern m_pattern = Pattern.compile("([\\w\\-]([\\.\\w])+[\\w]+@([\\w\\-]+\\.)+[A-Za-z]{2,4})");
                Matcher m_matcher = m_pattern.matcher(editText.getText().toString().trim());
                if (!m_matcher.matches() &&  editText.getText().toString().trim().length() > 0)
                {
                    m_isValid = false;
                    editText.setError(p_invalidMsg);
                }
                else
                {
                    m_isValid = true;
                }
            }
            else
            {
                m_isValid = false;
            }
        }
        else
        {
            m_isValid = false;
        }
    }
    catch(Throwable p_e)
    {
        p_e.printStackTrace(); // Error handling if application crashes
    }
    return m_isValid;
}
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • -1, don't use simple regexes to validate email addresses. That regex will not validate perfectly valid email addresses, including, say, the `.museum` TLD. – Charles Jan 02 '14 at 20:51