user1924104
user1924104

Reputation: 901

inputText empty check

I am using JSF, I have an h:inputText text box,

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

and I wish when the user presses the submit button,

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

for it to check if there is a value entered in the input text box and that it is over 6 characters in length

how would i do this ?

code i have tried :

<script type="text/javascript"> 

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

    </script> 

with this :

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

and I still am unable to get the script to run

Upvotes: 0

Views: 5532

Answers (2)

Hope4You
Hope4You

Reputation: 1947

You need to change =< to <= in the code.

I don't know JSF syntax, but you can try changing these three areas:

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

If you just need the one field validated.

Upvotes: 2

SJuan76
SJuan76

Reputation: 24780

Since you are using JSF, you should stick to JSF validation when possible.

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

A couple of links

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/

Upvotes: 3

Related Questions