0

I have tried using this code snippet for email validation but it doesnt seem to work or show any validations.

   EmailAddressEditField email=new EmailAddressEditField("Email Address: ", "");

        String address =email.getText();
        int at = address.indexOf("@");
        int len = address.length();
        String host = address.substring(at + 1, len);
        int dot = host.lastIndexOf('.');
        len = host.length();

        if (at <= 0 || at > len - 6  && dot < 0 || dot >= len - 3)
            Dialog.alert("Invalid email");
        else
        {
             if (host.indexOf("..") >= 0)
             {
                 Dialog.alert("Invalid email");
             }
             else
             {
                 //correct mail id.. continue your process

             }
        }

After i add(email); it gives me a dialog error of invalid email as soon as the form is opened.Please suggest me a proper email validation for a textfield/emailaddresseditfield which shows validation as soon as wrong input is typed in the field.Thanks

NOTE: The above code was taken from a previous query of similar pattern from stackoverflow.http://stackoverflow.com/questions/7580257/validation-for-email-in-blackberry Do not suggest any redirects to that same answer.Thanks.

Michael Donohue
  • 11,776
  • 5
  • 31
  • 44
learning_fly
  • 382
  • 1
  • 2
  • 11
  • possible duplicate of [@ validation for email in BlackBerry](http://stackoverflow.com/questions/7580257/validation-for-email-in-blackberry) – Michael Donohue May 12 '12 at 07:03
  • I have noted above that i am looking out for a different answer than that link.And the answer provided there gives me an error on run time.Please read the query correctly before downvoting. – learning_fly May 12 '12 at 09:22
  • Your goals are in contradiction. An empty string is an invalid email address, but you want to "show validation as soon as wrong input is typed" which is exactly what your code is doing. The problem with your goals is that the prefix of a valid email address is not valid, so you can't force the user to have a valid email in the box at all times. – Michael Donohue May 13 '12 at 19:24
  • I feel all the more confused at the moment with this email implementation thing.Are u aware of any other method of imposing an email validation. – learning_fly May 14 '12 at 07:29

2 Answers2

2

I'm not sure, how it works on BlackBerry, but for email validation I've always used regexp expressions. Here is an example.

Sebastian Łaskawiec
  • 2,667
  • 15
  • 33
0

Try this snippet of code (similar to your code, but modified slightly):

/**
   * Validates an email address. Checks that there is an "@"
   * in the field and that the address ends with a host that
   * has a "." with at least two other characters after it and
   * no ".." in it. More complex logic might do better.
   */
  public boolean isDataValid() {
    String address = email.getText();
    int at = address.indexOf("@");
    int len = address.length();
    if (at <= 0 || at > len - 6) return false;
      String host = address.substring(at + 1, len);
    len = host.length();
    if (host.indexOf("..") >= 0) return false;
    int dot = host.lastIndexOf(".");
    return (dot > 0 && dot <= len - 3);
  }

Then call this method, with it returning true or false based on the result.

biddulph.r
  • 5,226
  • 3
  • 32
  • 47