PLEASE -- why doesn't this validate function work? PLEASE?

K

KathyB

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;
}
 
G

Grant Wagner

KathyB said:
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 <[email protected]>

* Client-side Javascript and Netscape 4 DOM Reference available at:
*
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/frames.html

* Internet Explorer DOM Reference available at:
*
http://msdn.microsoft.com/workshop/author/dhtml/reference/dhtml_reference_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-developer/upgrade_2.html
 
K

Kathy Burke

Thanks Grant. I appreciate your taking the time to explain the "how" to
me. I'm definitely new at the javascript stuff and your answer helped me
a lot. Believe it or not, I put together what I have from books and
postings...it all looked so easy!

Thanks again,

Kathy
 
G

George M Jempty

KathyB said:
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.");

What if the user enters " "? Isn't that a blank entry too? This is
the number one mistake I see with roll your own form validation routines
-- including a lot I've seen in books. This is why I so often recommend
the routine you can pick up at:

http://pengoworks.com/index.cfm?action=get:qforms.

It's kind of like recommending CGI.pm on comp.lang.perl.misc. Though I
would be interested if anybody has seen anything better, as good, or
even nearly as good as the above.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top