Solved

Javascript Form Validation

Posted on 2013-06-21
1
357 Views
Last Modified: 2013-11-19
Hi,
I have a javascript for doing some form validation on form submit. when I submit the form for the first time it does all the form validation and pops up an alert, and comes back on the same form, however if I click on the submit form again nothing happens, the form does not submit, unless I reload the form. Below is the JS code, appreciate if someone can help me figure out what is wrong, I have to call location.reload to make this work, which refreshes the page and the user has to enter all the data again
function validateForm(){

		var rptdPos = document.getElementById('input_21').value;
		var NPos = document.getElementById('input_20').value;
		var bScore = document.getElementById('input_24').value;
		var bSupport = document.getElementById('input_25').value;
		var excludeInfo = document.getElementById('input_62').value;
		var exReason = document.getElementById('input_63').value;
	
		if(excludeInfo == 'Y' &&  exReason == ''){
			alert("Exclusion reason is required to be entered");
			location.reload();
			return false;
		}
		if(excludeInfo == 'N'){
			if((resinPlace == 'Y') &&((resType0 == '' && resType1 == '' && resType2 == '') || (resJustification0 == '' & resJustification1 == '' && resJustification2 == '' && resJustification3 == '' && resJustification4 == '') || resCat == '')){
				alert("Please enter values for Type,Category and Justfy");
				location.reload();
				return false;
			}
			if((bScore == '1' || bScore == '2' || bScore == '3') && bSupport == ''){
				alert("Please enter a value for BSupport");
				location.reload();
				return false;
			}
		}
		return true;
		//alert("reached end of javascript");
		
}

Open in new window

Calling the above javascript in my php form as below
<form class="Cform" action="submitPatientInfo.php"  onSubmit="return validateForm()" method="post" name="form_31" id="31" accept-charset="utf-8">

Open in new window

Also I have two buttons in the form Submit and Cancel, the javascript validation is called even if I hit the Cancel button, I want to call the validation JS only for Submit button and not Cancel button, the submit buttons are
<input type="submit" value="Submit Survey for Patient" name="submitPatient" class="form-submit-button form-submit-button-blue" />
<input type="submit" id = "input_2013" value="Cancel - Do not save" name="Return" class="form-submit-button form-submit-button-simple_red" />

Open in new window

0
Comment
Question by:Mohit_t
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 39267995
You've an error inside your validateForm function
So it run the first time, throw an error and stop working
so when you try the second time the function is not valid and don't run

A simple test page here work for me : http://jsfiddle.net/SMGFE/1/
Please note I define some global variable I did not saw in your current code :
var resinPlace = "Y";
var resType0 = "";
var resType1 = "";
var resType2 = "";
var resJustification0 = "";
var resJustification1 = "";
var resJustification2 = "";
var resJustification3 = "";
var resJustification4 = "";
var resCar = "";

Open in new window


Are you sure to have them?
Could you provide a link to see your page?
If not, start from the test page until your update fail
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jQuery scrollTop 2 40
If-Then-Else ASP problem 6 46
Show banners that are blocked by Ad Muncher 10 43
How to set a countdown by not using the PC time in PHP 36 43
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to create an extensible mechanism for linked drop downs.
This video teaches users how to migrate an existing Wordpress website to a new domain.
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)

948 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

21 Experts available now in Live!

Get 1:1 Help Now