2013-07-25 54 views
0

有没有一种方法,在richfaces中,ajaxvalidator或ajax支持用户在inputText中写入数据,只要他写入数字以外的内容即可。 换句话说,当用户输入NaN时,是否可以禁用inputText字段。jsf inputText验证程序

例如:如果用户试图写:“11a”,有没有办法阻止用户写a? 所以当他按下“a”警告输入不是数字时会出现。

这是JSF代码:

<h:inputText id="price" value="#{carBB.price}" required="true"> 
    <rich:ajaxValidator event="onkeypress"/>     
</h:inputText> 

这是在bean代码:

@NotNull 
private double price; 

public double getPrice() { 
return price; 
} 

public void setPrice(int price) { 
this.price = price; 
} 

在我的代码,当用户写的NaN但这并不妨碍他出现的警告写它。

+1

为什么不阻止输入文字的通过JavaScript的手段?你需要什么服务器? – skuntsel

+0

这是一个更大的应用程序,只是整个页面的一种形式。 – alan

回答

0

我结束了这一点,对我工作的罚款只允许双打:\

在标题:

<script type="text/javascript"> 
function disableKeys(e,exist) 
{ 
    var unicode=e.keyCode; 
    var value = exist.value; 
    if((unicode< 0x30 && unicode != 0x2E) || unicode> 0x39) 
    { 
     alert("Invalid Input should be number or \".\""); 
     return false; 
    } 
    else if(unicode == 0x2E && value.indexOf(".")>=0) 
    { 
     alert("Invalid Input already contains a \".\""); 
     return false; 
    } 
    else 
    { 
     return true; 
    } 

} 
</script> 

在车身:

<h:inputText id="price" value="#{carBB.price}" onkeypress="return disableKeys(event,this);"/> 
0
<h:inputText id="myId" value="#{myBean.value}" 
      onkeypress="if((event.which &lt; 48 &amp;&amp; event.which != 44 &amp;&amp; event.which != 46) || event.which &gt; 57) return false;"/> 

这将允许数字,逗号和点。

+0

我认为这不会做,因为该字段是'double',用户可能也想添加'.'符号。 –

+0

是的,我试过它没有工作,使用字母仍然工作 – alan