For registering a user I want them to be able to use a student email address something ending in .ac.uk but have tried a few emailCharacter strings but keeps returning invalid email address.
private Boolean validateEmail() {
String entry = regEmail.getEditText().getText().toString();
//Characters accepted for email address
String emailCharacters = "[a-zA-Z0-9+._%-+]{1,256}" +
"@" +
"[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" +
"(" +
"." +
"[a-zA-Z0-9][a-zA-Z0-9-]{0,25}" +
")+";
// "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]";
//If empty display error
if (entry.isEmpty()) {
regEmail.setError("Email field cannot be empty");
return false;
//If email address does not follow constraints
} else if (!entry.matches(emailCharacters)) {
regEmail.setError("Invalid email address");
return false;
UPDATE:
//Validation for email
private Boolean validateEmail() {
String entry = regEmail.getEditText().getText().toString();
//Characters accepted for email address
String emailCharacters = "[a-zA-Z0-9+._%-+]{1,256}" +
"@" +
"[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" +
"(" +
"." +
"[a-zA-Z0-9]{0,25}" +
"."+
"[a-zA-Z0-9]{0,25}" + // add these item more in your regex
")+";
Change your regex to this :
For trying your regex use this https://regex101.com/
//regex for conditions .ac.uk
[a-zA-Z0-9+._%-+]{1,256}@[a-zA-Z0-9]{0,64}(.[a-zA-Z0-9]{0,25}.[a-zA-Z0-9]{0,25})
private Boolean validateEmail() {
String entry = regEmail.getEditText().getText().toString();
//Characters accepted for email address
String emailCharacters = "[a-zA-Z0-9+._%-+]{1,256}" +
"@" +
"[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" +
"(" +
"." +
"[a-zA-Z0-9]{0,25}" +
"."+
"[a-zA-Z0-9]{0,25}" + // add these item more in your regex
")+";
// "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]";
//If empty display error
if (entry.isEmpty()) {
regEmail.setError("Email field cannot be empty");
return false;
//If email address does not follow constraints
} else if (!entry.matches(emailCharacters)) {
regEmail.setError("Invalid email address");
return false;
} else if(entry.matches(emailCharacters))
return true;
}