0

我曾尝试使用此代码片段进行电子邮件验证,但它似乎不起作用或显示任何验证。

   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

             }
        }

在我添加(电子邮件)之后;一旦打开表单,它就会给我一个无效电子邮件的对话框错误。请建议我对文本字段/电子邮件地址编辑字段进行适当的电子邮件验证,一旦在字段中输入错误的输入就会显示验证。谢谢

注意:上面的代码取自 stackoverflow.http://stackoverflow.com/questions/7580257/validation-for-email-in-blackberry 的类似模式的先前查询不要建议任何重定向到相同的答案。谢谢。

4

2 回答 2

2

我不确定它在 BlackBerry 上是如何工作的,但对于电子邮件验证,我一直使用正则表达式。这是一个例子

于 2012-05-11T17:38:51.080 回答
0

试试这段代码(类似于您的代码,但稍作修改):

/**
   * 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);
  }

然后调用此方法,根据结果返回 true 或 false。

于 2013-01-10T15:09:05.220 回答