In this post, we will learn how to do email validation in Android using Kotlin. We will create one function that will check and validate different types of email addresses. We have two different ways to do a email validation : By using a regular expressionn or regex and by using Android's utility class.
For Mobile Validation. For Valid Mobile You need to consider 7 digit to 13 digit because some country have 7 digit mobile number. If your main target is your own country then you can match with the length. Assuming India has 10 digit mobile number.
Java:
public static boolean isValidEmail(CharSequence target) {
return (!TextUtils.isEmpty(target) && Patterns.EMAIL_ADDRESS.matcher(target).matches());
}
Kotlin:
fun CharSequence?.isValidEmail() = !isNullOrEmpty() && Patterns.EMAIL_ADDRESS.matcher(this).matches()
Edit: It will work On Android 2.2+ onwards !!
Edit: Added missing ;
To perform Email Validation we have many ways,but simple & easiest way are two methods.
1- Using EditText(....).addTextChangedListener
which keeps triggering on every input in an EditText box
i.e email_id is invalid or valid
/**
* Email Validation ex:- [email protected]
*/
final EditText emailValidate = (EditText)findViewById(R.id.textMessage);
final TextView textView = (TextView)findViewById(R.id.text);
String email = emailValidate.getText().toString().trim();
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
emailValidate .addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
if (email.matches(emailPattern) && s.length() > 0)
{
Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show();
// or
textView.setText("valid email");
}
else
{
Toast.makeText(getApplicationContext(),"Invalid email address",Toast.LENGTH_SHORT).show();
//or
textView.setText("invalid email");
}
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// other stuffs
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
// other stuffs
}
});
2- Simplest method using if-else
condition. Take the EditText box string using getText() and compare with pattern provided for email. If pattern doesn't match or macthes, onClick of button toast a message. It ll not trigger on every input of an character in EditText box . simple example shown below.
final EditText emailValidate = (EditText)findViewById(R.id.textMessage);
final TextView textView = (TextView)findViewById(R.id.text);
String email = emailValidate.getText().toString().trim();
String emailPattern = "[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+";
// onClick of button perform this simplest code.
if (email.matches(emailPattern))
{
Toast.makeText(getApplicationContext(),"valid email address",Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getApplicationContext(),"Invalid email address", Toast.LENGTH_SHORT).show();
}
I did this way:
Add this method to check whether email address is valid or not:
private boolean isValidEmailId(String email){
return Pattern.compile("^(([\\w-]+\\.)+[\\w-]+|([a-zA-Z]{1}|[\\w-]{2,}))@"
+ "((([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\."
+ "([0-1]?[0-9]{1,2}|25[0-5]|2[0-4][0-9])\\.([0-1]?"
+ "[0-9]{1,2}|25[0-5]|2[0-4][0-9])){1}|"
+ "([a-zA-Z]+[\\w-]+\\.)+[a-zA-Z]{2,4})$").matcher(email).matches();
}
Now check with String of EditText:
if(isValidEmailId(edtEmailId.getText().toString().trim())){
Toast.makeText(getApplicationContext(), "Valid Email Address.", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "InValid Email Address.", Toast.LENGTH_SHORT).show();
}
Done
Use this method for validating your email format. Pass email as string , it returns true if format is correct otherwise false.
/**
* validate your email address format. [email protected]
*/
public boolean emailValidator(String email)
{
Pattern pattern;
Matcher matcher;
final String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
pattern = Pattern.compile(EMAIL_PATTERN);
matcher = pattern.matcher(email);
return matcher.matches();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With