@ validation for email in BlackBerry

Tag: blackberry , blackberry-simulator Author: wycaaaaa111 Date: 2011-09-12

I am developing a form where the user needs to enter a valid email. He/she will use it as a loginid to play the game.I want to know how can I validate the username containing @, similar to string.contains function. I learnt we can use string.indexOf() but it is not supporting "@". Kindly suggest how do I carry out this validation. When the email editfield loses focus, the username must be checked to see if it contains "@" or not.

What is wrong in asking a genuine question? Why the downvote? This is very dishonouring

Best Answer

Use EmailAddressEditField.. here is the way

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

                 }
            }

comments:

When I enter abc...it says invalid...ok agreed....when I enter [email protected] then also it says invalid..but registration succeeds...how can I make sure invalid doesnt come when user enters @
Please note I am not concerned about '.' here. I am stressing more upon @
It sounds to me that what you really want to do is ensure that the data entered is a deliverable email address. the EmailAddresEditField, or the associated filter, will only ensure that what is entered is syntactically correct. If you want to validate the email address you will have to do that on the server, probably by some use of the SMTP protocol.
No richard, I understand the level of complexity is high when it comes to validating a deliverable email id...all i want is to validate the entered data contains @ and .
Following is the validation condition that I am using. When I enter an email having ".co.in." it throws an error....I want to put the condition that, if email has @ and dot....accept...if email has 2 dots...den also accept int at = loginid.indexOf("@"); int len = loginid.length(); String host = loginid.substring(at + 1, len); int dot = host.lastIndexOf('.'); len = host.length(); if (at <= 0 || at > len - 6 && dot < 0 || dot >= len - 3) //if(host.indexOf("..") >= 0) Dialog.alert("Invalid email");

Other Answer1

please try to use EmailAdressEditField class