0

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

BenMorel
  • 34,448
  • 50
  • 182
  • 322
user1924104
  • 891
  • 2
  • 16
  • 38

2 Answers2

3

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/

SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • Thanks i have tried this but get `/index.xhtml @55,65 Parent not an instance of EditableValueHolder: javax.faces.component.html.HtmlForm@2434b3fb ` – user1924104 Dec 24 '12 at 18:00
  • Note that the `validateLenght` tag is nested *inside* the `inputText` tag. You cannot do `` – SJuan76 Dec 24 '12 at 18:02
  • Great thanks i have got it working, is there any way i can have the error message pop up in a window instead ? – user1924104 Dec 24 '12 at 18:18
  • This reverts to handling `FacesMessage` (or `` and ``). The subject is broad, one way is to use `component.isValid` property; other option is to use a component library that already provides solutions (check out the "Growl" example at primefaces showcase). The disadvantage of my method over plain JS is that at the beginning it is more complicated; but the advantage is that you end managing validation messages as any other message from your JSF application. – SJuan76 Dec 24 '12 at 22:40
  • Ok thanks, can i not use any js for this ? i have tried a few different validation codes and still none of them will run, i also notice it will not accept <=, can growl validate a `h:inputText` ? this is the only piece of validation i need to do on the site – user1924104 Dec 24 '12 at 22:50
2

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.

Hope4You
  • 1,927
  • 4
  • 21
  • 45
  • Thanks i just have an issue where inputtx is never used, how does the onsubmit call this function ? – user1924104 Dec 24 '12 at 18:04
  • hi there, i have tried this code but still can not call it for some reason, the original question is updated with the new code – user1924104 Dec 24 '12 at 18:41