Returning from function (FALSE VALUE) & FOCUS()

M

mtek

Hi,

I call a function with the following code:

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

Then, I validate the data:

if (emptyvalidation(thisform.Add_Data.CustName,"Customer Name is
empty")==false) {thisform.Add_Data.CustName.focus(); return false;};

The 'emptyvalidation' function is simple:

function emptyvalidation(entered, alertbox) {
with (entered) {
if (value==null || value=="" || value==" ")
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}

When it returns from the function, if the value is FALSE, then the
screen clears and the word 'false' is printed onto the screen. If I
put a document.write after the call to emptyvalidation, it prints what
I ask it to on the blank screen.

My question: Why is print the word FALSE?? I just need the form to
return focus to the text field I stated and that is it.....

Thanks!


John.
 
V

VK

Hi,

I call a function with the following code:

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

And what a link may possibly have to do with a form?

javascript:formvalidation(this) - which is javascript: pseudo-protocol
means: "call formvalidation function and send to it a reference to
this clicked link; then use output from formvalidation as HTML source
of a dynamically generated page to navigate". As your formvalidation
returns nothing but false, you are hapilly arriving to a new page
"<HTML><BODY>FALSE</BODY></HTML>" because browser considers your
"false" output as a badly broken page - and being pre-programmed to be
as sorry as possible for each broken page in the Internet it
automatically adds <HTML> and <BODY> for you.

<form method="POST" action="my.cgi"
onsubmit="return formvalidation(this)">
<!-- other elements -->
<input type="submit">
</form>
 
M

mtek

And what a link may possibly have to do with a form?

javascript:formvalidation(this) - which is javascript: pseudo-protocol
means: "call formvalidation function and send to it a reference to
this clicked link; then use output from formvalidation as HTML source
of a dynamically generated page to navigate". As your formvalidation
returns nothing but false, you are hapilly arriving to a new page
"<HTML><BODY>FALSE</BODY></HTML>" because browser considers your
"false" output as a badly broken page - and being pre-programmed to be
as sorry as possible for each broken page in the Internet it
automatically adds <HTML> and <BODY> for you.

<form method="POST" action="my.cgi"
 onsubmit="return formvalidation(this)">
 <!-- other elements -->
 <input type="submit">
</form>


Hi,

I can see where your example would work. But, I've been asked not to
use the standard SUBMIT button. They want an HTML link to perform
this action, which is why I coded as such.

Can you suggest another way, using the HTML link?

Thanks!

John
 
M

mtek

And what a link may possibly have to do with a form?

javascript:formvalidation(this) - which is javascript: pseudo-protocol
means: "call formvalidation function and send to it a reference to
this clicked link; then use output from formvalidation as HTML source
of a dynamically generated page to navigate". As your formvalidation
returns nothing but false, you are hapilly arriving to a new page
"<HTML><BODY>FALSE</BODY></HTML>" because browser considers your
"false" output as a badly broken page - and being pre-programmed to be
as sorry as possible for each broken page in the Internet it
automatically adds <HTML> and <BODY> for you.

<form method="POST" action="my.cgi"
 onsubmit="return formvalidation(this)">
 <!-- other elements -->
 <input type="submit">
</form>



Hi,

I can see where your example would work. But, I've been asked not to
use the standard SUBMIT button. They want an HTML link to perform
this action, which is why I coded as such.

Can you suggest another way, using the HTML link? I want it to
validate the different fields, and stop and focus when it finds one
with an error:

if (emptyvalidation(thisform.Add_Data.CustName,"Customer Name is
empty")==false) {thisform.Add_Data.CustName.focus();return false};
if (emptyvalidation(thisform.Add_Data.Comments,"Instructions are
empty")==false) {thisform.Add_Data.Comments.focus();return false};
.
.
.


Thanks!


John
 
E

Evertjan.

wrote on 23 mrt 2008 in comp.lang.javascript:
I call a function with the following code:

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

Then, I validate the data:

if (emptyvalidation(thisform.Add_Data.CustName,"Customer Name is
empty")==false) {thisform.Add_Data.CustName.focus(); return false;};

The 'emptyvalidation' function is simple:

function emptyvalidation(entered, alertbox) {
with (entered) {
if (value==null || value=="" || value==" ")
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}

When it returns from the function, if the value is FALSE, then the
screen clears and the word 'false' is printed onto the screen. If I
put a document.write after the call to emptyvalidation, it prints what
I ask it to on the blank screen.

My question: Why is print the word FALSE?? I just need the form to
return focus to the text field I stated and that is it.....

try this file:

========= test.html ==========

<a href="javascript:false;">SAVE DATA</a>

==============================

That is what href="javascript:.... was ment for in the early days,
to rewrite the html of a page with the returning javascript value,
if any.

So

never, Never, NEVER !!!!!!!!!!!!!!!!!!!!!!

use this href="javascript:.... construct again,
unless you really understand the above.
[Most of us, including me, don't.]

use graceful degradable:

<a href='sorryNoJavascript.html'
onclick = 'return doWhatYouMust();'
SAVE DATA</a>

and have doWhatYouMust() return false.

Or use another onclickable element, like a button, an image, etc.
 
M

mtek

 wrote on 23 mrt 2008 in comp.lang.javascript:




I call a function with the following code:
<a href="javascript:formvalidation(this);">SAVE DATA</a>
Then, I validate the data:
if (emptyvalidation(thisform.Add_Data.CustName,"Customer Name is
empty")==false) {thisform.Add_Data.CustName.focus(); return false;};
The 'emptyvalidation' function is simple:
function emptyvalidation(entered, alertbox) {
  with (entered) {
    if (value==null || value=="" || value==" ")
    {if (alertbox!="") {alert(alertbox);} return false;}
    else {return true;}
  }
}
When it returns from the function, if the value is FALSE, then the
screen clears and the word 'false' is printed onto the screen.   If I
put a document.write after the call to emptyvalidation, it prints what
I ask it to on the blank screen.
My question:  Why is print the word FALSE??  I just need the form to
return focus to the text field I stated and that is it.....

try this file:

========= test.html ==========

<a href="javascript:false;">SAVE DATA</a>

==============================

That is what href="javascript:.... was ment for in the early days,
to rewrite the html of a page with the returning javascript value,
if any.

So

never, Never, NEVER !!!!!!!!!!!!!!!!!!!!!!

use this href="javascript:.... construct again,
unless you really understand the above.
[Most of us, including me, don't.]

use graceful degradable:

<a href='sorryNoJavascript.html'
onclick = 'return doWhatYouMust();'
SAVE DATA</a>

and have doWhatYouMust() return false.

Or use another onclickable element, like a button, an image, etc.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)- Hide quoted text -

- Show quoted text -


Ok,

So, I want the form validated. And, if all the fields pass, then I
want to run a PHP script to save the data.

At the end of my vaildation function, I have this:

if (emptyvalidation(thisform.Add_Data.CustName,"Customer Name is
empty")==false) {thisform.Add_Data.CustName.focus();return false;};
if (emptyvalidation(thisform.Add_Data.Comments,"Instructions are
empty")==false) {thisform.Add_Data.Comments.focus();return false;};
submit()

So, what I am trying to accomplish is that the form is validated. If
something fails, it stops, displys the alert, and places focus in that
field, then stops.

If it passes all checks, then it submits the form to the PHP
script.....

Is this possible?

Thanks for your help.

John
 
V

VK

Hi,

I can see where your example would work. But, I've been asked not to
use the standard SUBMIT button. They want an HTML link to perform
this action, which is why I coded as such.

Can you suggest another way, using the HTML link?

A link is not a part of form, so "this" is out of use here. If your
form has a name, say <form name="myform" ... then:

<a href="noscript.html" onclick="
/* Prevent link navigation first: */
if ('preventDefault' in event) {
event.preventDefault();
}
else {
event.returnValue = false;
}
/* Now take your time to validate: */
formvalidation(document.forms['myform']);
">Validate</a>

It is more difficult if you have unnamed forms on your page. In this
case use index value, starting from 0 for the first form on the page
from the top:

<a href="noscript.html" onclick="
/* Prevent link navigation first: */
if ('preventDefault' in event) {
event.preventDefault();
}
else {
event.returnValue = false;
}
/* Now take your time to validate: */
formvalidation(document.forms[0]);
">Validate</a>
 
M

mtek

I can see where your example would work.  But, I've been asked not to
use the standard SUBMIT button.  They want an HTML link to perform
this action, which is why I coded as such.
Can you suggest another way, using the HTML link?

A link is not a part of form, so "this" is out of use here. If your
form has a name, say <form name="myform" ... then:

<a href="noscript.html" onclick="
 /* Prevent link navigation first: */
 if ('preventDefault' in event) {
  event.preventDefault();
 }
 else {
  event.returnValue = false;
 }
 /* Now take your time to validate: */
 formvalidation(document.forms['myform']);
 ">Validate</a>

It is more difficult if you have unnamed forms on your page. In this
case use index value, starting from 0 for the first form on the page
from the top:

<a href="noscript.html" onclick="
 /* Prevent link navigation first: */
 if ('preventDefault' in event) {
  event.preventDefault();
 }
 else {
  event.returnValue = false;
 }
 /* Now take your time to validate: */
 formvalidation(document.forms[0]);
 ">Validate</a>- Hide quoted text -

- Show quoted text -

Ok, and forgive me for being a bit stupid, but when you refer to
'event', is that the actual word 'event' or does it represent
something in my form?

My old code looked like this:

<FORM NAME='Add_Data' ACTION='savedata.php' METHOD=POST
onsubmit="return formvalidation(this)">
.
.
.
<a href="javascript:document.Add_Data.action =
'savedata.php';document.Add_Data.submit();">SAVE DATA</a>

Would that work?

Thanks!

John
 
M

mtek

I can see where your example would work.  But, I've been asked not to
use the standard SUBMIT button.  They want an HTML link to perform
this action, which is why I coded as such.
Can you suggest another way, using the HTML link?

A link is not a part of form, so "this" is out of use here. If your
form has a name, say <form name="myform" ... then:

<a href="noscript.html" onclick="
 /* Prevent link navigation first: */
 if ('preventDefault' in event) {
  event.preventDefault();
 }
 else {
  event.returnValue = false;
 }
 /* Now take your time to validate: */
 formvalidation(document.forms['myform']);
 ">Validate</a>

It is more difficult if you have unnamed forms on your page. In this
case use index value, starting from 0 for the first form on the page
from the top:

<a href="noscript.html" onclick="
 /* Prevent link navigation first: */
 if ('preventDefault' in event) {
  event.preventDefault();
 }
 else {
  event.returnValue = false;
 }
 /* Now take your time to validate: */
 formvalidation(document.forms[0]);
 ">Validate</a>- Hide quoted text -

- Show quoted text -


I'll try your code. I assume that 'event' is the actual word
'event'?? Also, it seems to be missing a '(', at least that is what
the browser says.....
 
M

mtek

I can see where your example would work.  But, I've been asked not to
use the standard SUBMIT button.  They want an HTML link to perform
this action, which is why I coded as such.
Can you suggest another way, using the HTML link?

A link is not a part of form, so "this" is out of use here. If your
form has a name, say <form name="myform" ... then:

<a href="noscript.html" onclick="
 /* Prevent link navigation first: */
 if ('preventDefault' in event) {
  event.preventDefault();
 }
 else {
  event.returnValue = false;
 }
 /* Now take your time to validate: */
 formvalidation(document.forms['myform']);
 ">Validate</a>

It is more difficult if you have unnamed forms on your page. In this
case use index value, starting from 0 for the first form on the page
from the top:

<a href="noscript.html" onclick="
 /* Prevent link navigation first: */
 if ('preventDefault' in event) {
  event.preventDefault();
 }
 else {
  event.returnValue = false;
 }
 /* Now take your time to validate: */
 formvalidation(document.forms[0]);
 ">Validate</a>- Hide quoted text -

- Show quoted text -

Hi,

I'll try your code. I'm afraid that the validation routine may not
work anymore:

if (emptyvalidation(thisform.Add_Data.CustName,"Customer Name is
empty")==false) {thisform.Add_Data.CustName.focus();return false;};

It says that some 'Object' is not defined......
 
E

Evertjan.

wrote on 23 mrt 2008 in comp.lang.javascript:
On Mar 23, 12:32 pm, "Evertjan." <[email protected]>
wrote:

[please do not quote signatures on usenet]
Ok,

So, I want the form validated. And, if all the fields pass, then I
want to run a PHP script to save the data.

At the end of my vaildation function, I have this:

if (emptyvalidation(thisform.Add_Data.CustName,"Customer Name is
empty")==false) {thisform.Add_Data.CustName.focus();return false;};
if (emptyvalidation(thisform.Add_Data.Comments,"Instructions are
empty")==false) {thisform.Add_Data.Comments.focus();return false;};
submit()

So, what I am trying to accomplish is that the form is validated. If
something fails, it stops, displys the alert, and places focus in that
field, then stops.

If it passes all checks, then it submits the form to the PHP
script.....

Is this possible?

Yes, it is.

submit() in itself is not possible,
you will have to do anyForm.submit()

Try to write more visual friendly code
and code that does not make spurious nes lines on usenet.
Use ! in stread of == false.
Var out repeating objects to shorten the lines.

function myFunction() {
var temp = thisform.Add_Data;
if (!emptyvalidation(temp.CustName,'Customer Name is empty')) {
temp.CustName.focus();
return false;
};
if (!emptyvalidation(temp.Comments,'Instructions are empty')) {
temp.Comments.focus();
return false;
};
thisform.submit();
};


Howver I do not understand how you can have text inputs,
that are two nodes below the form, like in:

thisform.Add_Data.Comments

unless thisform is the container <div> name,
and Add_Data is the form name,
which seems silly.
 
R

Richard Cornford

VK said:
And what a link may possibly have to do with a form?

javascript:formvalidation(this) - which is javascript:
pseudo-protocol means: "call formvalidation function
and send to it a reference to this clicked link;
<snip>

The code in javascript pseudo-protocol HREFs is (and always has been)
execute in the global execution context, and so in such code the -
this - value will be a reference to the global object, not the link that
is clicked upon (nor the form that is suggested by the original code).

Richard.
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top