0

How can I validate my fields (eg: name, email, mobile).

I validated whether the fields are empty but I don't know how to check whether the format is valid or not

My code:

public void btnClick(View v)
{
    EditText name = (EditText) findViewById(R.id.signup_name);
    EditText email= (EditText) findViewById(R.id.signup_email);
    EditText phone= (EditText) findViewById(R.id.signup_phone);
    if(name.getText().length()==0)
    {
        name.setError("Field cannot be left blank.");
    }
    else if (email.getText().length()==0){
        email.setError("Field cannot be left blank.");
    }
    else if (phone.getText().length()==0){
        phone.setError("Field cannot be left blank.");
    }

}
N J
  • 27,217
  • 13
  • 76
  • 96
Razul
  • 99
  • 1
  • 12
  • possible duplicate of [How should I validate an e-mail address?](http://stackoverflow.com/questions/1819142/how-should-i-validate-an-e-mail-address) – Pankaj Jul 07 '15 at 07:26
  • You cannot validate emails reliably - and your business logic shouldn't rely on that anyway. The only thing you can do is what every other service does as well: Send an email to the address with a link in it which can be used to validate the email address. – Xaver Kapeller Jul 07 '15 at 07:47
  • try this library https://github.com/ragunathjawahar/android-saripaar for all form validation – arjunkn Jul 07 '15 at 09:10

3 Answers3

5

This facility is already provided by android itself

android.util.Patterns.EMAIL_ADDRESS.matcher(email.getText().toString()).matches();

It returns true if it is a valid email address and false if not

Vishwajit Palankar
  • 3,033
  • 3
  • 28
  • 48
  • This is just a bad idea. You cannot validate emails like this, it will never work reliably. Why do you think you have to activate your email address for every service by clicking on a link in an email you receive? That is the only way to be sure that the email is valid with the extra bonus of making sure that it exists as well. – Xaver Kapeller Jul 07 '15 at 07:44
  • yeah i didn't get you – Vishwajit Palankar Jul 07 '15 at 07:46
  • 1
    @XaverKapeller why do you think that piece of code exists? – the-ginger-geek Jul 07 '15 at 07:57
  • 1
    For the same reason that other ways to shoot yourself in the foot exist, but I can tell you now: It just doesn't work. No pattern could ever validate all possible email addresses. Very smart people have tried to construct patterns that reliably validate email addresses but it just doesn't work. Even complex patterns still fail on some email addresses. Even some hilariously simple ones. It just doesn't work, there really isn't anything more to say. – Xaver Kapeller Jul 07 '15 at 08:08
1
private boolean isValidEmail(String target) {
        return android.util.Patterns.EMAIL_ADDRESS.matcher(target).matches();
    }
Antrromet
  • 15,294
  • 10
  • 60
  • 75
  • This is just a bad idea. You cannot validate emails like this, it will never work reliably. Why do you think you have to activate your email address for every service by clicking on a link in an email you receive? That is the only way to be sure that the email is valid with the extra bonus of making sure that it exists as well. – Xaver Kapeller Jul 07 '15 at 07:45
  • 1
    @XaverKapeller Thats a server side validation that you're talking about. I don't see anywhere in the OPs question where hes asking about that. I just gave the simplest solution which can be done on local side. – Antrromet Jul 07 '15 at 07:48
  • You are missing my point: Validation on the client side does not work. It is as simple as that. No pattern can match all possible email addresses. The only thing that really works reliable on the client side is a pattern like `*@*` and using something like that defeats the purpose of validation anyway. So that's why I am saying: You cannot validate an email address unless you send an email there. – Xaver Kapeller Jul 07 '15 at 07:51
  • @XaverKapeller I totally agree with you. But my point is, suppose today a person is making an android app, with a basic login screen and then moving on to the next screen. Do you think, he should invest hours in building a server for his app first (that does nothing but email validation)? Or first basically complete the first prototype or the few screens and then move on. – Antrromet Jul 07 '15 at 07:55
  • 1
    @XaverKapeller Based upon the question, I think OP has just started with his/her app and wants to do a basic check, so that the user doesn't add some random stuff in email. I totally get your point where no amount of regex can exactly verify correct email format. But then you should also understand that sometimes these things can be over engineering. – Antrromet Jul 07 '15 at 07:58
  • @XaverKapeller What if OP is making an app for his school project? Do you think he should spend hours in adding server side validation and then forget about rest of the app? The question was asked about local validation and I gave the solution for that. Your idea for validation is for full fletched app (system), and it must be done, AGREED. But only when its really needed. – Antrromet Jul 07 '15 at 08:00
  • What's the point of implementing something that does not work? He could just as well not implement it or mock it instead of just copying and pasting code that does not work anyway. But more importantly: How is the email even relevant if there is no backend which uses the email for some purpose? And if he just wants the email of the person using the app he can use the email associated with the Google account on the Android phone. That email address does not require validation since it already has to be valid to be used. – Xaver Kapeller Jul 07 '15 at 08:01
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82569/discussion-between-antrromet-and-xaver-kapeller). – Antrromet Jul 07 '15 at 08:02
  • By providing solutions like this you are just telling people who don't know better that this is a perfectly valid solution for validating emails - which is not the case. The real issue is something completely different - client side validation is impossible and no business logic should rely on some email being validated like this. – Xaver Kapeller Jul 07 '15 at 08:02
  • Client side validation just does not make any sense. It just doesn't work and is the completely wrong approach. If your business logic was really thought through you wouldn't need any kind of email format validation. And I think this is at the core of the OP's question - he is asking about how to validate email format - which simply does not work and he shouldn't have to do that anyway. It just does not make sense. – Xaver Kapeller Jul 07 '15 at 08:05
  • @XaverKapeller Firstly, I'm totally against your use of default Google address, because to access that you'd need an additional permission which I think is totally unnecessary. And do you know what kind of an app OP is making? If suppose you get a school assignment to make an android app with a login screen and after all validations are done, move the user to the next screen. Are you going to spend hours in making a server to solve this school assignment? – Antrromet Jul 07 '15 at 08:10
  • @XaverKapeller So according to you around 80k people are all idiots, because thats how many people have viewed and used this question here. http://stackoverflow.com/questions/1819142/how-should-i-validate-an-e-mail-address – Antrromet Jul 07 '15 at 08:11
  • No my point is just don't validate email addresses. It's just a waste of time since it does not work. If you use your solution you will just run into problems because the pattern will not match even basic email addresses just because they have a slightly unusual structure or characters in them. Just don't validate email addresses it does not make any sense. – Xaver Kapeller Jul 07 '15 at 08:13
  • The answer you link to says the same thing I am saying. Don't use Regex, it does not work. Look at the complex pattern in the accepted answer. Even that fails on some email addresses and that pattern is ten times more complex and accurate than `Patterns.EMAIL_ADDRESS`. – Xaver Kapeller Jul 07 '15 at 08:15
  • @XaverKapeller Your solution is clearly a case of overengineering. And regarding the link that I posted earlier, please read all the answers below too, past the first solution. For instance, the second solution as mentioned [here](http://stackoverflow.com/a/7882950/451951) to the same question has been voted by more than 500 people. – Antrromet Jul 07 '15 at 08:18
  • @XaverKapeller Try searching on Google, "validate email android" the first link that comes up is http://stackoverflow.com/questions/1819142/how-should-i-validate-an-e-mail-address. And unfortunately you did not read past the first answer, but I'm pretty sure others would. And if you are so righteous about server side email validation then maybe you should comment on each and every answer [here](http://stackoverflow.com/questions/1819142/how-should-i-validate-an-e-mail-address) regarding your point. And lets see how the world responds to that. – Antrromet Jul 07 '15 at 08:21
  • @XaverKapeller Also sir, finally please take a look [here](https://en.wikipedia.org/wiki/Overengineering). Its getting late here, otherwise I'd love to hear your thoughts further on this. You have a good day/night! – Antrromet Jul 07 '15 at 08:22
  • My solution is not bothering to try and validate email addresses. How is that over engineering? All I am saying is validating emails on the client side does not make any sense at all and it doesn't work anyway. If you want you can just try to match email addresses with `Patterns.EMAIL_ADDRESS` and you will quickly find email addresses which won't work. Tell me: Does it really make sense to reject perfectly valid email addresses just because a pattern says so? Why would you intentionally exclude part of your user base just because their email address is a little unusual? – Xaver Kapeller Jul 07 '15 at 08:23
  • Just don't bother validating email addresses. There is no point on the client side anyway. – Xaver Kapeller Jul 07 '15 at 08:23
  • And I read past the first answer of the question you link to, but I don't understand what you want me to see there? All there is is more people posting answers which use a pattern to match email addresses - which is a bad idea. Just because there are many answers on Stack Overflow suggesting something even if they are highly upvoted does not mean it is a good idea. Or do you thing you can find good cryptographic advice in the [php] tag? – Xaver Kapeller Jul 07 '15 at 08:34
  • I think you misunderstood me from the beginning. I never suggested the OP should actually implement a backend - I just said that that is the only thing that works. My position has always been: Just don't validate email addresses on the client side, it does not work and makes no sense whatsoever anyway. Trying to implement email validation on the client side is just wasted time because it will never work and using some pattern just means that you are barring perfectly valid email addresses from being used. No pattern works perfectly and could ever match all possible emails. – Xaver Kapeller Jul 07 '15 at 08:37
1

There is a library called JavaMail API, which enables you, among other things, to validate email addresses. Download it here, add it to your project, and then use the following method to ensure validation:

public static boolean isValidEmailAddress(String email) {
    boolean result = true;
    try {
        InternetAddress emailAddr = new InternetAddress(email);
        emailAddr.validate();
    } catch (AddressException ex) {
        result = false;
    }
    return result;
}
Răzvan Barbu
  • 189
  • 4
  • 16
  • further information about the projet can be found here https://java.net/projects/javamail/pages/Home#Project_Documentation – Răzvan Barbu Jul 07 '15 at 08:16