Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Test form values before submit

Posted on 2012-04-04
2
Medium Priority
?
219 Views
Last Modified: 2012-04-06
Ive been playing about trying to validate a simple form, and I cannot get it to fail if the Option box still says 'Please Select', or if there are no comments.

My code Im running to test is:-
<script type="text/javascript">
        function submitForm() {
        	var $formIssue = false;
       	
        	if ($("#Category").val == "Please Select")
        		{
        		alert("Please select a category");	
        		$formIssue = true;        			
        		}
       	if (($("#Comments").val == "") && ($formIssue == false))
        		{
       		alert("Please enter some comment, or descibe the issue");	     
        		$formIssue = true;    			
        		}		        		
        	if ($formIssue == false)
        		{
        		$("#btnSubmit").attr("disabled", true); 
	            	$.post("Help_submit.php", $("#helpRequest").serialize(), 
			function(data) { 
				alert(data); 
		                    	window.location.href = "index.php"; 
		                	});
		}		        			
	}
</script>

Open in new window


And the Form is quite simple:-
<form id="helpRequest" action="Help_submit.php" method="post">		
	<select name="Category">
		<option>Please Select</option>
		<option>Site Appearance</option>
		<option>Login</option>
		<option>Certificate Download</option>
		<option>Certificate Upload</option>
		<option>Missing certificate</option>
		<option>Other Issue/suggestion</option>
	</select> 				

	<textarea name="Comments" rows="8" cols="45"></textarea>
	<input type="button" id="btnSubmit" value="Submit" onclick="submitForm();" />	
</form>

Open in new window


I just dont understand, why it keeps submitting fine, where if the option group says 'Please Select' and there is nothing in the comments, then it should fail striaght away.

Any ideas what Im doing wrong?
0
Comment
Question by:tonelm54
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
SANDY_SK earned 2000 total points
ID: 37808117
There are two issues

1.  when you using $("#Category") it is looking for an element with the id as "category but in your code your select has only the name attribute

2. the call is $("#Category").val()  the parenthesis is missing

see corrected code below
<script src="jquery.js"  type="text/javascript"></script>
<script type="text/javascript">
        function submitForm() {
        	var $formIssue = false;

        	if ($("#Category").val() == "Please Select")
        		{
        		alert("Please select a category");
        		$formIssue = true;
        		}
       	if (($("#Comments").val() == "") && ($formIssue == false))
        		{
       		alert("Please enter some comment, or descibe the issue");
        		$formIssue = true;
        		}
        	if ($formIssue == false)
        		{
        		$("#btnSubmit").attr("disabled", true);
	            	$.post("Help_submit.php", $("#helpRequest").serialize(),
			function(data) {
				alert(data);
		                    	window.location.href = "index.php";
		                	});
		}
	}
</script>


<form id="helpRequest" action="Help_submit.php" method="post">
	<select id="Category" name="Category">
		<option>Please Select</option>
		<option>Site Appearance</option>
		<option>Login</option>
		<option>Certificate Download</option>
		<option>Certificate Upload</option>
		<option>Missing certificate</option>
		<option>Other Issue/suggestion</option>
	</select>

	<textarea name="Comments" rows="8" cols="45"></textarea>
	<input type="button" id="btnSubmit" value="Submit" onclick="submitForm();" />
</form>

Open in new window

0
 
LVL 17

Expert Comment

by:nanharbison
ID: 37808391
So you have to add id="Category" to the select element as Sandy said:

<form id="helpRequest" action="Help_submit.php" method="post">
      <select id="Category" name="Category" id="Category">
            <option>Please Select</option>
            <option>Site Appearance</option>
            <option>Login</option>
            <option>Certificate Download</option>
            <option>Certificate Upload</option>
            <option>Missing certificate</option>
            <option>Other Issue/suggestion</option>
      </select>

      <textarea name="Comments" rows="8" cols="45"></textarea>
      <input type="button" id="btnSubmit" value="Submit" onclick="submitForm();" />
</form>
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question