0

I am working with JSF 2 and I want to validate an inputText with javascript, in my script is defined that only numbers can be entered but the letters are also entered

my code

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;
  } 

}

Thanks for all, excuse me for my English

Ronald
  • 291
  • 3
  • 7
  • 23
  • You can use primefaces extension: http://www.primefaces.org/showcase-ext/views/inputNumber.jsf;jsessionid=4tqh7174yoj7xqp6tzoihjps – Silwest Dec 07 '14 at 10:45

1 Answers1

1

Like I said in the comment the best way would be to use primefaces extention numericinput. However if you want to do that using Java Script I found this solution:

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

More info can be found under this topic: HTML Text Input allow only Numeric input

Community
  • 1
  • 1
Silwest
  • 1,620
  • 1
  • 15
  • 29