Solved

Test form values before submit

Posted on 2012-04-04
2
182 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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 …
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…

747 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

13 Experts available now in Live!

Get 1:1 Help Now