Ronald
Ronald

Reputation: 301

JSF 2 - JavaScript inputText Validation Number

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

Upvotes: 0

Views: 1296

Answers (1)

Silwest
Silwest

Reputation: 1620

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

Upvotes: 1

Related Questions