我正在使用 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;
}
}
谢谢大家, 请原谅我的英语
最佳答案
就像我在评论中所说,最好的方法是使用 primefaces 扩展numericinput
。但是,如果您想使用 Java 脚本来做到这一点,我找到了这个解决方案:
<input type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57'></input>
更多信息可以在本主题下找到: HTML Text Input allow only Numeric input
关于javascript - JSF 2 - JavaScript 输入文本验证号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27300606/