When a user tries to register or login in the app, you have to check if the entered email address is a valid email id or not, that is if it is in the correct format or not. To check for the validation you can make use of the following source codes and steps:
Step 1: In the register activity you can include the statements that are as follows:
EditText emailTxt = (EditText) findViewById(R.id.etEmail);
EditText pwdTxt = (EditText) findViewById(R.id.etPassword);
if (emailTxt.length() == 0) {c
Toast.makeText(RegisterActivity.this, “Email field cannot be empty”, Toast.LENGTH_LONG).show();
}
if (pwdTxt.length() == 0) {
Toast.makeText(RegisterActivity.this, “Password field cannot be empty”, Toast.LENGTH_LONG).show();
}
This code displays a toast message when the email field is empty or when the password field is left empty. The message can be displayed for a short time by using LENGTH_SHORT instead of LENGTH_LONG.
Step 2: The code below uses SharedPreferences which is an interface used for accessing and modifying data to store username and password between activities. For a more complex app you will have to use a database instead.
if (emailTxt.length() != 0 && pwdTxt.length() != 0) {
//check if email is valid
if (isValidEmail(emailTxt.getText())) {
String userNameString = emailTxt.getText().toString();
String pwdString = pwdTxt.getText().toString();
Toast.makeText(RegisterActivity.this, “Registration successful!”, Toast.LENGTH_LONG).show();
SharedPreferences shoppingAdviserPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());// getPreferences(Context.MODE_APPEND);
SharedPreferences.Editor editor = shoppingAdviserPreferences.edit();
editor.putString(“username”, userNameString);
editor.putString(“password”, pwdString);
editor.commit();
finish();
} else {
Toast.makeText(RegisterActivity.this, “Invalid Email”, Toast.LENGTH_LONG).show();
}
}
}
});
}
public final static boolean isValidEmail (CharSequence target) {
if (target == null)
return false;
else {
return Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
}
The EMAIL_ADDRESS format is already been given in android studio. In the above code Patterns.EMAIL_ADDRESS.matcher(target).matches(); is a method in the android library which does the email address validation for us.
The first characters should be the name or the name and any numerical digits the user wants which is followed by the @ symbol followed by the mail servers name followed by a dot which is again followed by the com , org or in etc.
Step 3: After the codes are implemented we can run our app and see how it works. We can enter the email address and press on the register button, which will check the format of the email address. If the fields are empty then it will display a toast message that the fields are empty and if the email address is not in the correct format then it will display the toast message that the email address is invalid. If it’s valid then it displays a toast message saying that the registration is successful.
This is how a isvalidEmail function works.
Step 4: After these codes are being implement the screen will be displayed like the following with the toast messages shown at the end of the screen: