[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

Test form values before submit

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
tonelm54
Asked:
tonelm54
1 Solution
 
SANDY_SKCommented:
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
 
nanharbisonCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now