469,306 Members | 1,881 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

form submits to blank page - novice question

HI All,

I have a function that validates a form, when the function returns true or
false it still does not submit the form. I am using a <div> tag with text to
submit the form.
I have tried the following ways to make the form submit & validate at the
same time;

1. I have tried the onsubmit function at the top of the form, this seems to
only work with a form button or an image.
2. I have tried to call the function at the href level and this seems to
make the form validate, but the form still submits to a blank page.

I have verified that the function is working with a form button, however I
would like to use a text link with CSS styles around it if possible, couls
someone help me out with the syntax please?

Thanks in advance for your answer

Sean

function validateForm(form) {

var p = form.elements["nameoncard"].value;

var s = form.elements["credit_card"].value;
if (checkGoodCard(s) && p != "")

return true;

else

alert("One or more of your details is invalid or missing, please make sure
that you have entered in your name and or the correct credit card number.");

return false;

}


<form method="post" name="elements" action="test.asp" onsubmit="return
validateForm(this)">
<input type="text" name="credit_card">
<input name="nameoncard" type="text">

<div="sidebar"><ul><li><a
href="javascript:validateForm(this)">continue</a></li></ul>
</form>
Jul 20 '05 #1
4 5630
I think your problem lies with the href

<a href="javascript:validateForm(this)">continue</a>

why not change this to

<a href="javascript:document.elements.submit()">conti nue</a>

"sean" <se********@shopsmart.com.au> wrote in message
news:3f******@dnews.tpgi.com.au...
HI All,

I have a function that validates a form, when the function returns true or
false it still does not submit the form. I am using a <div> tag with text to submit the form.
I have tried the following ways to make the form submit & validate at the
same time;

1. I have tried the onsubmit function at the top of the form, this seems to only work with a form button or an image.
2. I have tried to call the function at the href level and this seems to
make the form validate, but the form still submits to a blank page.

I have verified that the function is working with a form button, however I
would like to use a text link with CSS styles around it if possible, couls
someone help me out with the syntax please?

Thanks in advance for your answer

Sean

function validateForm(form) {

var p = form.elements["nameoncard"].value;

var s = form.elements["credit_card"].value;
if (checkGoodCard(s) && p != "")

return true;

else

alert("One or more of your details is invalid or missing, please make sure
that you have entered in your name and or the correct credit card number.");
return false;

}


<form method="post" name="elements" action="test.asp" onsubmit="return
validateForm(this)">
<input type="text" name="credit_card">
<input name="nameoncard" type="text">

<div="sidebar"><ul><li><a
href="javascript:validateForm(this)">continue</a></li></ul>
</form>

Jul 20 '05 #2
I have tried this, but when I submit the form the function does not work! Do
you have any other Ideas?
"steve stevo" <st***@stevosteve.fsnet.co.uk> wrote in message
news:bo**********@newsg4.svr.pol.co.uk...
I think your problem lies with the href

<a href="javascript:validateForm(this)">continue</a>

why not change this to

<a href="javascript:document.elements.submit()">conti nue</a>

"sean" <se********@shopsmart.com.au> wrote in message
news:3f******@dnews.tpgi.com.au...
HI All,

I have a function that validates a form, when the function returns true or false it still does not submit the form. I am using a <div> tag with text
to
submit the form.
I have tried the following ways to make the form submit & validate at

the same time;

1. I have tried the onsubmit function at the top of the form, this seems

to
only work with a form button or an image.
2. I have tried to call the function at the href level and this seems to
make the form validate, but the form still submits to a blank page.

I have verified that the function is working with a form button, however I would like to use a text link with CSS styles around it if possible, couls someone help me out with the syntax please?

Thanks in advance for your answer

Sean

function validateForm(form) {

var p = form.elements["nameoncard"].value;

var s = form.elements["credit_card"].value;
if (checkGoodCard(s) && p != "")

return true;

else

alert("One or more of your details is invalid or missing, please make sure that you have entered in your name and or the correct credit card

number.");

return false;

}


<form method="post" name="elements" action="test.asp" onsubmit="return
validateForm(this)">
<input type="text" name="credit_card">
<input name="nameoncard" type="text">

<div="sidebar"><ul><li><a
href="javascript:validateForm(this)">continue</a></li></ul>
</form>


Jul 20 '05 #3
Lee
sean said:
<a href="javascript:validateForm(this)">continue</a>
Thanks for asking. We don't get enough opportunities to tell
people not to abuse the "javascript:" protocol this way.

The "javascript:" protocol is used to replace the current contents
of the page with the value of the Javascript expression that
follows the colon. In this case, that's the value returned by
your validateForm() funtion, which apparently displays as blank.

In some cases, people can get away with using the side effect of
evaluating the expression to do something useful in a function,
but submitting a form isn't usually one of those cases.

There's also the problem that the "this" keyword, when used in
an URL like that will NOT be a reference to your form, so your
validation function won't work.

If you must a text link to submit the form, you could use:

<a href="someOtherPageThatApologizesForRequiringJavaS cript.html"
onclick="if(validateForm(document.elements)){docum ent.elements.submit()};return
false"continue</a>


Jul 20 '05 #4
steve stevo wrote:
I think your problem lies with the href

<a href="javascript:validateForm(this)">continue</a>

why not change this to

<a href="javascript:document.elements.submit()">conti nue</a>
Because

<a href="javascript:document.forms['elements'].submit()"
onclick="document.forms['elements'].submit(); return false"continue</a>


is, if required, better than that, since it is backed up by W3C recommendations.
PointedEars

Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Param R. | last post: by
7 posts views Thread by Chuck Anderson | last post: by
5 posts views Thread by Advo | last post: by
6 posts views Thread by 0utlawza | last post: by
Frinavale
13 posts views Thread by Frinavale | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.