2012-12-24 33 views
0

我使用JSF,我有一個H:inputText的文本框中,inputText的空管檢查

<h:form> 
        Please enter your username: 
        <h:inputText value="#{user.id}"/><br/><br/> 

,當用戶按下提交按鈕,我想,

<h:commandButton value="Submit" action="upload/uploadText"/> 

爲它檢查在輸入文本框中輸入一個值,並且它的長度超過6個字符

我該怎麼做?

碼我曾嘗試:

<script type="text/javascript"> 

     function required(){ 
      if (document.getElementById("Username").value.length == 0) 
      { 
       alert("message");   
       return false; 
      }  
      return true; 
     } 

    </script> 

與此:

<h:body> 

       <h:form onsubmit="return required();"> 
        Please enter your username: 
        <h:inputText id="Username" value="#{user.id}"> 
        </h:inputText><br></br><br></br> 

        To print a piece of text, please press the submit button below to upload the text:<br/><br/> 
        <h:commandButton type="submit" value="Submit" action="upload/uploadText"/> 
       </h:form> 

      </h:body> 

,我仍然感到無法獲得腳本運行

+0

第一步:你看了關於JavaScript驗證? [你有什麼嘗試](http://whathaveyoutried.com)? – epascarello

+0

我已閱讀關於js驗證,我只是不知道它如何檢查只是輸入文本框,郵編上面的代碼我已經試過 – user1924104

+0

這稍微好一點。你打電話給'required'怎麼樣? – epascarello

回答

2

您需要在改變=<<=碼。

我不知道JSF的語法,但你可以嘗試改變這三個領域:

<h:inputText name="myTextField" id="myTextField" /><br/><br/>

<h:form onsubmit="return required();">

function required(){ 
    if (document.getElementById("myTextField").value.length <= 6) 
     { 
     alert("message");   
     return false; 
     }  
     return true; 
} 

如果你只需要驗證的一個領域。

+0

謝謝:),更新了原來的問題 – user1924104

+0

謝謝我只是有一個問題,inputtx從來沒有使用過,onsubmit如何調用這個函數? – user1924104

+0

@ user1924104我的錯誤。現在已修復 – Hope4You

3

由於您使用的是JSF,因此您應該儘可能堅持使用JSF驗證。

<h:inputText id="Username" value="#{UserBean.userName}"> 
    <f:validateLength minimum="6" maximum="15"/> 
</h:inputText> 

的幾個環節

http://viralpatel.net/blogs/javaserver-faces-jsf-validation-tutorial-error-handling-jsf-validator/

http://www.mkyong.com/jsf2/customize-validation-error-message-in-jsf-2-0/

+0

謝謝我已經試過這個,但得到'/index.xhtml @ 55,65 父不是EditableValueHolder:javax.faces.component.html的一個實例。HtmlForm @ 2434b3fb ' – user1924104

+0

請注意,'validateLenght'標記嵌套在* inputText標記中。你不能這樣做'' – SJuan76

+0

非常感謝我得到它的工作,有沒有什麼辦法可以讓我的錯誤消息彈出窗口相反? – user1924104