473,509 Members | 2,950 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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;
}
Jul 20 '05 #1
3 3539
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
Jul 20 '05 #2
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

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
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.");


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.

Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1636
by: lmeng | last post by:
Hi, I am new to this Forum. Thanks in advance for any kind help. In the following HTML code, when I change the value of one text field then click "Modify" button, if the validation fails a...
2
2119
by: Fabri | last post by:
I would like to ask you the following: I use Macromedia Dreamweaver as an editor for HTML and Js. It also writes some js functions to simply validate forms. I always used it with no bugs. ...
7
3570
by: x muzuo | last post by:
Hi guys, I have got a prob of javascript form validation which just doesnt work with my ASP code. Can any one help me out please. Here is the code: {////<<head> <title>IIBO Submit Page</title>...
6
3132
by: Daniel Rimmelzwaan | last post by:
I want to send a biztalk document to an aspx page, and I need to see some sample code, because I just can't make it work. I have a port with transport type HTTP, pointing to my aspx page, something...
7
2160
by: Stephen | last post by:
I have some code which I call from a custom validator however I seem to have got the logic wrong and im having trouble figuring out how to write my code to get things to work the way I require....
1
9586
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
2
1074
by: settyv | last post by:
Hi, I want to display Business exceptions in Messagebox in asp.net.For that i used MessageBox API function.But when i tried calling this function from UI,it is not displaying messagebox.Please...
3
1650
by: antred | last post by:
Hello everyone, While working on a program I encountered a situation where I'd construct a largish data structure (a tree) from parsing a host of files and would end up having to throw away...
0
7234
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7344
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7412
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7069
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4730
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3203
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
775
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.