Too many problems with javascript - Passing Forms

M

mtek

I may give this up.

I am passing a form object:

<a href="javascript:formvalidation(this);">SAVE DATA</a>

I receive the form:

function formvalidation(thisform) {
with (thisform) {

if (emailvalidation(CustEmail,"Illegal E-mail")==false)
{CustEmail.focus(); return false;};

But, the above line does not work. I thought that since I am passing
the form as an object reference and using the WITH keyword, I do not
have to refer to the form name or the .value property.......

Am I wrong? I just want to pass the form and refer to the fields by
name.....

John (Virgin to Javascript)
 
L

Lasse Reichstein Nielsen

I may give this up.

I am passing a form object:

<a href="javascript:formvalidation(this);">SAVE DATA</a>

Don't use links for submitting. Use submit-buttons. Then it works even
if Javascript is disabled, and users are used to buttons sumbitting
forms. And preferably put validation in the forms onsubmit handler,
so that all types of submits will be validated (e.g., pressing return
in an input element).

Also, never ever use "javascript:" URLs. Put the code in an onclick
handler instead.
I receive the form:

function formvalidation(thisform) {
with (thisform) {

if (emailvalidation(CustEmail,"Illegal E-mail")==false)
{CustEmail.focus(); return false;};

But, the above line does not work.

Elaborate, please. What symptoms does it show that leads you to conclude
that it doesn't work?
What do you expect it to do?

(Also notice that the returned false value isn't used for anything).
I thought that since I am passing
the form as an object reference and using the WITH keyword, I do not
have to refer to the form name or the .value property.......

We have no idea what your form looks like. I'm assuming a text input
field named "CustEmail". What does the function "emailvalidation"
expect to receive? An input element or a string value?
If it expects a string value, you *do* need to extract that from the
email input, i.e.,
... emailvalidation(CustEmail.value, "Illegal E-mail") ...
The "with" construction only makes properties of the form object
directly available. The value property of the input element
Am I wrong? I just want to pass the form and refer to the fields by
name.....

That is correct. I recommend against it, because using "with" will
make *all* properties of the object available as if they were local
variables, possibly shadowing existing variables. An input field
in my browser has ~180 properties.

Just write thisform.CustEmail. It's not that long, and it makes it
much clearer what "CustEmail" refers to.

/L
 
S

SAM

(e-mail address removed) a écrit :
I may give this up.

I am passing a form object:

<a href="javascript:formvalidation(this);">SAVE DATA</a>

Your link is in the form :

<a href="javascript:formvalidation(this.form);">SAVE DATA</a>

Your link is outside the form which is unique :

<a href="javascript:formvalidation(document.form[0]);">SAVE DATA</a>
 
L

Lee

(e-mail address removed) said:
I may give this up.

I am passing a form object:

<a href="javascript:formvalidation(this);">SAVE DATA</a>

I receive the form:

function formvalidation(thisform) {
with (thisform) {

if (emailvalidation(CustEmail,"Illegal E-mail")==false)
{CustEmail.focus(); return false;};

But, the above line does not work. I thought that since I am passing
the form as an object reference and using the WITH keyword, I do not
have to refer to the form name or the .value property.......

Am I wrong? I just want to pass the form and refer to the fields by
name.....


This HTML:

href="javascript:formvalidation(this);"

instructs the browser to discard the current contents and replace
them with the value returned by the formvalidation() function.
That's not what you want.

Using the "with" statement like that should allow you to refer to
the controls by name, but that's not good practice. It won't allow
you to leave off the .value property. Since we don't know anything
about your emailvalidation() function, we don't know whether it
expects a reference to a form control or to the value of that
form control.


--
 

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,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top