Email Validation on EditText in android

2019-07-16 15:53发布

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

7条回答
甜甜的少女心
2楼-- · 2019-07-16 16:53

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();
     }
}
查看更多
登录 后发表回答