最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - inputText empty check - Stack Overflow

programmeradmin1浏览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:mandButton 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:mandButton type="submit" value="Submit" action="/upload/uploadText"/>
                </h:form>

            </h:body>

and I still am unable to get the script to run

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:mandButton 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:mandButton type="submit" value="Submit" action="/upload/uploadText"/>
                </h:form>

            </h:body>

and I still am unable to get the script to run

Share Improve this question edited Dec 29, 2013 at 15:05 BenMorel 36.7k52 gold badges206 silver badges337 bronze badges asked Dec 24, 2012 at 17:30 user1924104user1924104 9012 gold badges18 silver badges38 bronze badges 8
  • First step: Have you read about JavaScript validation? What have you tried? – epascarello Commented Dec 24, 2012 at 17:32
  • i have read about js validation, i just am not sure how it checks just that inputtext box, post code above of code i have tried – user1924104 Commented Dec 24, 2012 at 17:35
  • That is slightly better. How are you calling required? – epascarello Commented Dec 24, 2012 at 17:37
  • i have not called it yet, 'onsubmit="required()">' but i am unsure where to place this – user1924104 Commented Dec 24, 2012 at 17:40
  • Now there is your real question! – epascarello Commented Dec 24, 2012 at 17:41
 |  Show 3 more ments

2 Answers 2

Reset to default 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/blogs/javaserver-faces-jsf-validation-tutorial-error-handling-jsf-validator/

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

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.

发布评论

评论列表(0)

  1. 暂无评论