2014-12-04 29 views
0

我与JSF 2的工作,我想验证一个inputText的使用JavaScript,在我的脚本定义,只有号码可以输入,但字母也进入JSF 2 - JavaScript的inputText的评估数字

我的代码

JSF

<p:inputText id="idDNI" style="width:140px;" value="#{empleadoBean.emVista.strDNI}" maxlength="8" required="true" onkeypress ="validar(event)"/> 

JS

function validar(e) { 
    var unicode=e.keyCode? e.keyCode : e.charCode; 

    if (unicode == 8 || unicode == 46 || unicode == 37 || unicode == 39) { 
     return true; 
    } 
    else if (unicode < 48 || unicode > 57) { 
     return false; 
    } 
    else{ 
     return true; 
    } 

}

感谢所有, 原谅我的英语

+0

您可以使用primefaces扩展:HTTP://www.primefaces。组织/陈列柜-EXT /视图/ inputNumber.jsf; JSESSIONID = 4tqh7174yoj7xqp6tzoihjps – Silwest 2014-12-07 10:45:33

回答

1

就像我在评论说,最好的办法是使用primefaces extention numericinput。但是,如果你想要做的,使用Java脚本,我发现这个解决方案:

<input type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57'></input> 

更多信息可以在这个主题下找到: HTML Text Input allow only Numeric input