问题
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.
回答1:
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
}
}
回答2:
please try to use EmailAdressEditField class
来源:https://stackoverflow.com/questions/7580257/validation-for-email-in-blackberry