Help validating multiple text fields with error messages
I am very new to JQuery. I am trying to create a basic form where the user will: type a name, enter comments in a textbox, click a radio button, click a check box. If the user does not do one or more of these things, I need an error message to appear besides the box or button on the particular area that is missing. I have everything working for the user name. I don't know how to work in that I need validation on all the other fields. I have tried adding another function below the first one, or adding names separated by a comma in the first function. Anything I have tried so far has made the page dysfunctional. Thanks for taking a look.
- <html>
- <head>
- <title>Basic Validation</title>
- <script src="http://code.jquery.com/jquery-latest.js">
- </script>
-
- <script>
- $(function(){
- $('#theButton')
- .click(function(event) {
- if ($("#userName").val()==""){
- alert("empty!");
- $("#errUserName").text("Needs a user name!").removeClass("hideError").addClass("showError");
- }else{
- $("#errUserName").text("").removeClass("showError");
- }
- });
- });
- </script>
- <style>
- .showError{
- display:block;
- color:#ff0000;
- }
- .hideError{
- display:none;
- color:#ff0000;
- }
- </style>
- </head>
- <body>
- <form>
- <label>User Name:</label><BR>
- <input type="text" id="userName" /><span id="errUserName" class="hideError">dsa</span>
- <br>Tell us about yourself.<br>
- <textarea name="comments" cols="25" rows="5"></textarea>
-
- <div id="jobLike"><br>
- Do you like your job?<BR>
- Yes<input type="radio" name="answerYes" value="1" />
- No<input type="radio" name="answerNo" value="2" />
- <p>
- Why?
- <br>
- <input type="checkbox" name="thePay" />Pay<br>
- <input type="checkbox" name="theBenefits" />Benefits<br>
- <input type="checkbox" name="theFlexibility" />Flexibility<br>
- <input type="checkbox" name="hateJob" />None, I hate it!<br>
- <p>
- <input type="button" id="theButton" value="Validate!" />
- </form>
- </body>
- </html>