View Full Version : form validation check before the page is submitted to asp

11-10-2007, 02:07 AM
1) Script Title: Required Field Validation

2) Script URL (on DD):
<script language="JavaScript">

* Required field(s) validation v1.10- By NavSurf
* Visit Nav Surf at http://navsurf.com
* Visit http://www.dynamicdrive.com/ for full source code

function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("name", "email", "employer", "address", "citystatezip", "phone");
// Enter field description to appear in the dialog box
var fieldDescription = Array("Name", "Email", "Employer", "Address", "City State Zip," "Phone");
// dialog message
var alertMsg = "Please complete the following fields:\n";

var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired[i]];
if (obj){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
alertMsg += " - " + fieldDescription[i] + "\n";
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " - " + fieldDescription[i] + "\n";
case "text":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\n";
if (obj.type == undefined){
var blnchecked = false;
for (var j = 0; j < obj.length; j++){
if (obj[j].checked){
blnchecked = true;
if (!blnchecked){
alertMsg += " - " + fieldDescription[i] + "\n";

if (alertMsg.length == l_Msg){
return true;
return false;
// -->

3) Describe problem: I am using this script however it doesn't validate the form first before going to the asp mail form

which is here: <form action="http://www.voicesforhousing.org/authorize.asp" method="post" name="FormMail" onsubmit="return formCheck(formobj);" id="FormMail">

How can I get the form to validate before it goes to the authorize.asp file or is there a way to put this information into the .asp file?

Thank you for your help.

11-10-2007, 07:55 AM
Warning: Please include a link to the DD script in question in your post. See this thread (http://www.dynamicdrive.com/forums/showthread.php?t=6) for the proper posting format when asking a question. Also, format your code using the CODE tag please for easier reading.

Are you saying you're having trouble getting the script to work like in the demo (http://www.dynamicdrive.com/dynamicindex16/requiredcheck.htm), that is to say, stop the form submission when one of the required fields isn't filled out? Based on the above the script seems set up correctly; please post a link to the page on your site that contains the problematic script so we can check it out.

11-10-2007, 05:05 PM
Sorry for not including the information correctly, this is my first time posting.

Here is the page with the form set up: http://www.coastalhousing.org/letterofauthorization.html

The form works, however it doesn't validate the required fields.

11-11-2007, 08:02 AM
There is at least one error as far as I can see in the way you've set up the script. The following line:

var fieldDescription = Array("Name", "Email", "Employer", "Address", "City State Zip," "Phone");

The comma in red should appear on the outside of the text "City State Zip", so:

var fieldDescription = Array("Name", "Email", "Employer", "Address", "City State Zip", "Phone");

11-12-2007, 07:45 PM
I fixed the comma, however, the form is still not validating the required fields in the Javascript before it is being sent to the .asp file. Any other suggestions?

11-12-2007, 09:37 PM
Ah yes, there is another error, in your HTML:

onsubmit="return formCheck(formobj);"

"formobj" should be changed to this instead:

onsubmit="return formCheck(this);"

11-13-2007, 01:16 AM
You are a genius! Thank you for all your help! :)