KathyB wrote:
Hi, I've tried everything I can think of. Using the script below, I
thought on submit my ConfirmSave function would run, then if true
returned, would run my form action.
At this point it appears to be skipping the function altogether. I'm
also unable to get the textbox.value since I need to use variable for
the form name and input element name. I'm fairly new to this...please
tell me where I am going astray...Thanks.
HTML element:
<form name="form_test"
action="parent.hidden.location=ValidateSave.aspx" method="post"
onsubmit="return ConfirmSave('form_test','measurement')">
<input type="text" name="measurement" value="{.}"/>
<input type="submit" value="SAVE"/>
</form>
FUNCTION:
function ConfirmSave(frm,input) 'arguments get generated via xsl
'for this example, they are
(form_test,measurement)
{
var formname="" +frm
var test="" +input
var content="" +input.value
//tested var's with alert box, frm and input ok, but not value!
if (document.formname.test.value=="")
{
alert("You are trying to save a blank entry.");
formname.test.focus();
formname.test.select();
return false;
}
else {
confirm("You are about to enter: " +formname.test.value+ " -- are
you sure?");
return false;
}
return true;
}
It's not working because you can't reference a form with just it's name
in most browsers. Even in browsers where you can use just the form name,
document.SomeVariableContainingTheNameOfAForm simply does not work.
What you basically have is:
var theFormName = 'blah';
var theInputName = 'bleh';
document.theFormName.theInputName.value ....
What you want is:
var theFormName = 'blah';
var theInputName = 'bleh';
document.forms[theFormName].elements[theInputName].value ...
As well, confirm() returns a boolean true/false depending on what they
selected, so you are calling confirm(), then returning false all the
time.
Basically, there's just a lot of little things that are wrong with the
script (such as why you are taking paramenters, concatenating an empty
string to them and assigning them to another variable).
You already have a reference to the form in the onsubmit event, so just
use:
<form ... onsubmit="return ConfirmSave(this, 'measurement');">
and
function ConfirmSave(formReference, fieldName) {
var theFormField = formReference.elements[fieldName];
if (theFormField.value == "") {
alert("You are trying to save a blank entry.");
theFormField.focus();
theFormField.select();
return false;
} else {
return confirm("You are about to enter: " +
theFormField.value +
" -- are you sure?");
}
}
--
| Grant Wagner <gw*****@agricoreunited.com>
* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/...ce/frames.html
* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/a...ence_entry.asp
* Netscape 6/7 DOM Reference available at:
*
http://www.mozilla.org/docs/dom/domref/
* Tips for upgrading JavaScript for Netscape 7 / Mozilla
*
http://www.mozilla.org/docs/web-deve...upgrade_2.html