Solved

Test form values before submit

Posted on 2012-04-04
2
193 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
2 Comments
 
LVL 11

Accepted Solution

by:
SANDY_SK earned 500 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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…

815 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now