【问题标题】:JSF 2 - JavaScript inputText Validation NumberJSF 2 - JavaScript inputText 验证号
【发布时间】:2014-12-04 17:48:13
【问题描述】:

我正在使用 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;
  } 

}

谢谢大家, 对不起我的英语

【问题讨论】:

标签: javascript jsf-2


【解决方案1】:

就像我在评论中所说,最好的方法是使用 primefaces 扩展 numericinput。但是,如果您想使用 Java 脚本来做到这一点,我找到了这个解决方案:

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

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

【讨论】:

    猜你喜欢
    • 2013-07-25
    • 2016-12-07
    • 1970-01-01
    • 2011-12-30
    • 2012-09-04
    • 1970-01-01
    • 2012-10-28
    • 2011-06-15
    • 1970-01-01
    相关资源
    最近更新 更多