Validate an email inside an EditText
Solution 1
Just change your regular expression as follows:
"[a-zA-Z0-9._-]+@[a-z]+\\.+[a-z]+"
Because . (dot) means match any single-char.ADD a double backslash before your dot to stand for a real dot.
Solution 2
I wrote a library that extends EditText which supports natively some validation methods and is actually very flexible.
Current, as I write, natively supported (through xml attributes) validation methods are:
- regexp: for custom regexp
- numeric: for an only numeric field
- alpha: for an alpha only field
- alphaNumeric: guess what?
- email: checks that the field is a valid email
- creditCard: checks that the field contains a valid credit card using Luhn Algorithm
- phone: checks that the field contains a valid phone number
- domainName: checks that field contains a valid domain name ( always passes the test in API Level < 8 )
- ipAddress: checks that the field contains a valid ip address webUrl: checks that the field contains a valid url ( always passes the test in API Level < 8 )
- nocheck: It does not check anything. (Default)
You can check it out here: https://github.com/vekexasia/android-form-edittext
Hope you enjoy it :)
In the page I linked you'll be able to find also an example for email validation. I'll copy the relative snippet here:
<com.andreabaccega.widget.FormEditText
style="@android:style/Widget.EditText"
whatever:test="email"
android:id="@+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/hint_email"
android:inputType="textEmailAddress"
/>
There is also a test app showcasing the library possibilities.
This is a screenshot of the app validating the email field.
Solution 3
public boolean validateEmail(String email) {
Pattern pattern;
Matcher matcher;
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();
}
Solution 4
If you are using API 8 or above, you can use the readily available Patterns
class to validate email. Sample code:
public final static boolean isValidEmail(CharSequence target) {
if (target == null) {
return false;
} else {
return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
}
}
By chance if you are even supporting API level less than 8, then you can simply copy the Patterns.java
file into your project and reference it. You can get the source code for Patterns.java
from this link
Solution 5
Several good options here including android.util.Patterns.EMAIL_ADDRESS for API 8+.
anonymous
Updated on August 11, 2022Comments
-
anonymous over 1 year
I want to validate an email introduced inside an EditText and this the code that I already have:
final EditText textMessage = (EditText)findViewById(R.id.textMessage);
final TextView text = (TextView)findViewById(R.id.text);
textMessage.addTextChangedListener(new TextWatcher() { public void afterTextChanged(Editable s) { if (textMessage.getText().toString().matches("[a-zA-Z0-9._-]+@[a-z]+.[a-z]+") && s.length() > 0) { text.setText("valid email"); } else { text.setText("invalid email"); } } public void beforeTextChanged(CharSequence s, int start, int count, int after) {} public void onTextChanged(CharSequence s, int start, int before, int count) {} });
The problem is that when I introduce 3 characters after the "@", it appears the message "valid email", when it must appear when I introduce the complete email.
Any suggerence?
Thank you all!
-
anonymous over 12 yearsBut I want to use that because if the user introduces something that is not an email e.g: example.com, he won't be able to hit the button in order to register himself. The example code that you post is only for the keyboard. Thank you for your quick reply, but it is not what I was looking for.
-
asenovm over 12 yearsthat's it, you've got to escape the dot
-
anonymous over 12 yearsIt worked perfectly! Thank you very much!
-
anonymous over 12 yearsThank you for the reply, but Yuanhai Shi's reply is easier to implement and works perfectly.
-
anonymous almost 12 yearsI'll take a look! Thank you! :)
-
Ken Y-N almost 12 years
-
Gaurav Arora over 11 yearsIt worked for me. Thanks
-
Jashan PJ almost 11 yearsTRIPLE LIKE..!! Nice work dude. Congrats..! :)
-
Andrea Baccega almost 11 yearsthanks ;) Really appreciated
-
Fawwad Shafi almost 11 yearsWorked for me Thanks :)
-
Sandeep V about 10 yearshave u used any specific jar file because when i copy the above code i am getting error in first line so asking @AndreaBaccega