2014-12-04 31 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