?
Solved

Test form values before submit

Posted on 2012-04-04
2
Medium Priority
?
212 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

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…
Suggested Courses

762 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