Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/gwt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java GWT中的验证_Java_Gwt - Fatal编程技术网

Java GWT中的验证

Java GWT中的验证,java,gwt,Java,Gwt,我正在使用GWT,希望使用java代码(即使用正则表达式)验证电子邮件,但在使用代码时: /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package org.ArosysLogin.client; import java.util.regex.Matcher; import java.util.regex.Pattern; public

我正在使用GWT,希望使用java代码(即使用正则表达式)验证电子邮件,但在使用代码时:

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ArosysLogin.client;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class EmailValidator{

      private Pattern pattern;
      private Matcher matcher;

      private static final String EMAIL_PATTERN =
                   "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

      public EmailValidator(){
          pattern = Pattern.compile(EMAIL_PATTERN);
      }

      /**
       * Validate hex with regular expression
       * @param hex hex for validation
       * @return true valid hex, false invalid hex
       */
      public boolean validate(final String hex){

          matcher = pattern.matcher(hex);
          return matcher.matches();


      }
}

。它在build.xml中给了我运行时错误。您能告诉我为什么会发生这种情况,以及它的解决方案是什么吗。

Java正则表达式在GWT中不可用。您应该使用。

根据文档,它不应该工作。但我意外地发现,您也可以使用java.lang.String的matches方法

所以,你可以这样做:

public boolean validate(final String hex){
  return ((hex==null) || hex.matches(EMAIL_PATTERN));
}

这是验证电子邮件id的代码。我已经检查过了。它在GWT中运行良好

String  s ="survi@gmail.com";
Boolean b = s.matches(
 "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
System.out.println("email is " + b);

试着这样做:

if(email.matches("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$")){
    GWT.log("Email Address Valid");
}else{
 GWT.log("Email Address Invalid");
 valid = false;
}

你能把你得到的stacktrace贴到控制台上吗?