Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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
Regex 验证程序正则表达式模式输入只接受2-5位数字_Regex_Validation_Jsf - Fatal编程技术网

Regex 验证程序正则表达式模式输入只接受2-5位数字

Regex 验证程序正则表达式模式输入只接受2-5位数字,regex,validation,jsf,Regex,Validation,Jsf,我想在JSF应用程序中使用自定义验证器验证用户的输入数据。插入的数据必须是2到5位之间的数字 小脸 <h:inputText id="num1" label="num1" required="true" size="5" maxlength="5" styleClass="#{component.valid ? '' : 'validation-failed'}" value="#{sumaManagedBean.number1}" requiredMessage="You must e

我想在JSF应用程序中使用自定义验证器验证用户的输入数据。插入的数据必须是2到5位之间的数字

小脸

<h:inputText id="num1" label="num1" required="true" size="5" maxlength="5" 
styleClass="#{component.valid ? '' : 'validation-failed'}"
value="#{sumaManagedBean.number1}"
requiredMessage="You must enter a value">
<f:validator validatorId="validators.NumberValidator"/>    
</h:inputText>
<h:message for="num1" />
验证器

@FacesValidator("validators.NumberValidator")
public class NumberValidator implements Validator
{
    private static final String NUMBER_PATTERN = "[0-9]{1,5}";

    private Pattern pattern;
    private Matcher matcher;

    public NumberValidator()
    {
        pattern = Pattern.compile(NUMBER_PATTERN);
    }

    @Override
    public void validate(FacesContext context, UIComponent component,Object value) throws ValidatorException 
    {
        String number = value.toString();

        //Only numeric characters
        matcher = pattern.matcher(value.toString());
        if(!matcher.matches())
        {
            FacesMessage msg = new FacesMessage("Only numeric characters");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);

        } 
        //Minimum length 2 numbers
        else if((number.length() < 2))  
        {  
            FacesMessage msg =  new FacesMessage("Minimum length 2 numbers");
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(msg);
        }  
    }     
}
@FacesValidator(“validators.NumberValidator”)
公共类NumberValidator实现验证器
{
私有静态最终字符串编号_PATTERN=“[0-9]{1,5}”;
私有模式;
私人匹配器匹配器;
公共号码识别器()
{
pattern=pattern.compile(数字模式);
}
@凌驾
公共void验证(FacesContext上下文、UIComponent组件、对象值)引发Validator异常
{
字符串编号=value.toString();
//仅限数字字符
matcher=pattern.matcher(value.toString());
如果(!matcher.matches())
{
FacesMessage msg=新的FacesMessage(“仅数字字符”);
消息设置严重性(FacesMessage.SEVERITY_错误);
抛出新的验证异常(msg);
} 
//最小长度2个数字
else if((number.length()<2))
{  
FacesMessage msg=新的FacesMessage(“最小长度2个数字”);
消息设置严重性(FacesMessage.SEVERITY_错误);
抛出新的验证异常(msg);
}  
}     
}
当我插入一位数字时,验证程序工作正常,并显示消息“最小长度2个数字”。但当我插入一些字母而不是像“eee3”这样的数字时,验证器会显示以下消息:“num1:‘eee3’必须是介于-2147483648和2147483647之间的数字。例如:9346”,此时它应该显示“仅数字字符”

我做错了什么

但是,当我插入一些字母而不是像“eee3”这样的数字时,验证器会显示以下消息:“num1:‘eee3’必须是介于-2147483648和2147483647之间的数字。例如:9346”

这是JSF内置的默认转换错误消息。当您将输入字段绑定到类型为
Integer
的bean属性或其原始对应项
int
时,它将透明地启动。JSF转换器将在JSF验证器之前运行。在发生任何转换错误时,验证程序都不会运行。换句话说,您根本不需要根据该数字正则表达式模式进行验证

您可以通过
converterMessage
属性自定义转换器消息

<h:inputText value="#{bean.integer}" ... converterMessage="Only numeric characters" />

与具体问题无关,在
验证器中执行
值。toString()
是一种不好的做法。不要那样做。您应该将提供的值参数强制转换为模型中声明的实际类型(bean属性)。然后,您很快就会意识到验证的某些部分是不必要的

Integer number = (Integer) value;

并且,
Matcher
实例是。您不应该将其声明为其实例可以跨多个线程共享的类的实例变量。

为什么不将输入转换为数字,并验证其是否>99,以及如何知道某个对象是线程不安全的,尽管在本例中明确说明,“此类的实例对于多个并发线程的使用是不安全的。”?@Tiny:
Matcher
是有状态且不同步的。即,它具有可由非同步实例方法操作的实例变量。
<h:inputText value="#{bean.integer}">
    <f:validateLongRange minimum="10" maximum="99999" />
</h:inputText>
<h:inputText value="#{bean.integer}" maxlength="5" required="true"
    requiredMessage="You must enter a value"
    converterMessage="Only numeric characters"
    validatorMessage="Minimum length 2 numbers">
    <f:validateLongRange minimum="10" maximum="99999" />
</h:inputText>
Integer number = (Integer) value;