Click to See Complete Forum and Search --> : Forms - validate & disable submit


solferz
04-06-2003, 03:25 PM
I want to use disable=true in the form 'onsubmit' to stop multiple form submissions... but I'm having trouble incorporating it into some existing Dreamweaver form validation.

My form has 20 text fields, each of which are required and must be numeric. As I mentioned, I've been using the js form validation which is built in to Dreamweaver which validates the fields as I need. My problem now is that when I add the extra code to disable the submit button, and the form input **isn't** valid, I get my usual text box error message and the submit button gets disabled even though nothing's being submitted - which means that the form can't then be corrected and submitted...

Sorry if I haven't explained that very well! If anyone can make any sense of it and know what I should be doing, I'd love to know how to get round this :)

S

khalidali63
04-06-2003, 04:33 PM
Guessing may not help you compltely,why not post your code so that some one can take a look and determine what is going on in your code and suggest a solution for you.

Cheers

Khalid

solferz
04-07-2003, 02:04 AM
Sorry, here's the validation code:

<!--

/* *************************************************************
* set some global variables that will be checked later *
***************************************************************/
var msg = ""; // an output message
var missing = ""; // for missing required fields
var invNum = ""; // for invalid numeric fields

/* *************************************************************
* The main validation function, calls other sub-functions *
***************************************************************/
function validate(frm)
{
for(i=0; i<frm.elements.length; i++) // loop through form elements
{
var el = frm.elements[i];
if(el.required) // if element has required property
{ // test to see if field is empty
if(isEmpty(el))
{
missing += "\n - " + el.name + " is a required field";
}
}
if(el.numeric)
{
if(notNumeric(el))
{
invNum += "\n - " + el.name + " must be a number";
}
}
}

// build output message
if(missing.length !=0 || invNum.length != 0)
{
if(missing.length !=0)
{
msg += "\n\nThe following required fields are missing:";
msg += missing;
}
if(invNum.length !=0)
{
msg += "\n\nYou entered incorrect numeric data in these fields:";
msg += invNum;
}
errMsg(msg); // call the output function
msg = ""; missing = ""; invNum = ""; // reset all our variables
return false;
}
else
{
return true;
}
}

/* *************************************************************
* Sub-functions follow from here to end of file *
* All sub-functions return true if field is of invalid *
* format and false if they are valid entries *
***************************************************************/
function isEmpty(field)
{
str = field.value;
if(str == "")
// make sure not to put a space between those quotes
{
return true;
}
else
{
for(j=0; j<str.length; j++)
{
if(str.charAt(j) != " ")
// make sure to put a space between those quotes!
{
return false;
}
}
}
return true;
}

function notNumeric(field)
{
var errCount = 0;
var numdecs = 0; // number of decimal points
for(j=0;j<field.value.length;j++)
{
c = field.value.charAt(j); // short hand notation for character at position j
if((c >= 0 && c <= 9) || c=="." || (j==0 && c == "-"))
{
if(c==".")
{
numdecs++; // count the number of decimal points
}
}
else
{
errCount++; // if it's none of those, increment error counter
break; // no need to continue looping, it's not a number
}
}
// error if count is non-zero or there are more than one decimal point
if(errCount > 0 || numdecs > 1)
{
return true;
}
return false;
}

function stripNonDigits(str)
{
newStr = "";
for(j=0; j<str.length; j++)
{
c = str.charAt(j);
if(c >= "0" && c <= "9")
{
newStr += c;
}
}
return newStr;
}


function errMsg(msg)
{
var theMsg = "You entered some incorrect values into the form. ";
theMsg += "Please correct your entries then re-submit the form.\n";
theMsg += "____________________________________________________________________";
theMsg += msg;
theMsg += "\n____________________________________________________________________\n";
alert(theMsg);
}


//-->


My form fields are named a1score, h1score thru to h10score, a10score and the following is the called in the form "onsubmit":

this.h1score.required=true;
this.a1score.required=true;
this.h2score.required=true;
this.a2score.required=true;
this.h3score.required=true;
this.a3score.required=true;
this.h4score.required=true;
this.a4score.required=true;
this.h5score.required=true;
this.a5score.required=true;
this.h6score.required=true;
this.a6score.required=true;
this.h7score.required=true;
this.a7score.required=true;
this.h8score.required=true;
this.a8score.required=true;
this.h9score.required=true;
this.a9score.required=true;
this.h10score.required=true;
this.a10score.required=true;
this.h1score.numeric=true;
this.a1score.numeric=true;
this.h2score.numeric=true;
this.a2score.numeric=true;
this.h3score.numeric=true;
this.a3score.numeric=true;
this.h4score.numeric=true;
this.a4score.numeric=true;
this.h5score.numeric=true;
this.a5score.numeric=true;
this.h6score.numeric=true;
this.a6score.numeric=true;
this.h7score.numeric=true;
this.a7score.numeric=true;
this.h8score.numeric=true;
this.a8score.numeric=true;
this.h9score.numeric=true;
this.a9score.numeric=true;
this.h10score.numeric=true;
this.a10score.numeric=true;
return validate(this);


I added "this.submit.disabled=true;" to the above, but obviously if there's an error in the validation, the submit button get's disabled...

Cheers
Sol

khalidali63
04-07-2003, 02:15 AM
Take a look at your validate functionit returns a true or false value.
if its true you expect it to submit else not.your requirements is when a form is returned true i.e it should be submitted then the submit buttin should be disabled?

If the above is correct then you shoudl calla function may be superValidator() in the onunload event call
in the superValidator() you can do this

function superValidator(){
if(superValidator()){ //if form should be submitted
document.formName.submitButtonName.disabled = true;

}
}

This should solve your problem

Cheers

Khalid

solferz
04-07-2003, 12:05 PM
Thanks Khalid.

The only problem I might have with onunload is that my form is on a php page will posts to itself, therefore the onunload event wouldn't be called when the form is submitted (would it?).

Sol.