/    Sign up×
Community /Pin to ProfileBookmark

Trying to validate form

I have two forms on one page. I want the 2nd form to call the validate function and check an input field in the 1st form and then if all goes well, submit the 2nd form. I’m having problems though. Here is the 2nd form

[code=html]<form id=”form_finish” name=”form_finish” method=”post” action=”order3.php” onSubmit=”return validate(this);”>

<a href=”javascript:validate(document.forms.form_finish);”><img src=”/images/orange_button_checkout.png” alt=”Check Out” border=”0″></a>

<input type=”image” src=”/images/orange_button_empty.png” name=”clear” value=”Empty Cart” />
</form>[/code]

…and here is my validate function which is in the <head> tags…

[CODE]function validate(form)
{
val = document.getElementsByName(“form_phrase”)[0].value

if(val == “” || val == null)
{
form.submit()
}
else
{
var name = confirm(“You have entered information for another band, but have not updated the cart. Would you like to continue anyway?”)
if(name==true)
{
form.submit()
}
}
}
[/CODE]

I get this error…

“form has no properties”

Any help would be greatly appreciated ?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@David_HarrisonSep 21.2005 — You don't need to say "form.submit();" a simple "return true;" would do it.
Copy linkTweet thisAlerts:
@tabzterSep 21.2005 — 2 things:

i) listen to David coz hes right.

ii) change [B]javascript:validate(document.forms.form_finish);[/B] to [B]javascript:validate(document.forms['form_finish']);[/B]
Copy linkTweet thisAlerts:
@chadsaunauthorSep 21.2005 — That did work, but it just showed true on the screen. What I want it to do is perform the actual action of the form, or submit the form. How might I do that?

By the way - when I use document.forms['form_finish'] what kind of properties do I have access to? Because I tried this (document.forms['form_finish'].method) and it said it didn't have any properties.
Copy linkTweet thisAlerts:
@David_HarrisonSep 21.2005 — Have a go with the attached code. Here's the HTML for the form extracted:&lt;form method="post" action="order3.php" onsubmit="return validate(this);"&gt;

&lt;p&gt;
&lt;input type="text" name="form_phrase"&gt;
&lt;/p&gt;

&lt;p&gt;
&lt;input type="image" src="/images/orange_button_checkout.png" alt="Checkout" name="checkout"&gt;
&lt;input type="image" src="/images/orange_button_empty.png" alt="Empty Cart" name="empty"&gt;
&lt;/p&gt;

&lt;/form&gt;
<input type="image"> will submit the form, so since there are effectively two submit buttons there then you would have to use PHP to detect which one was activated. Apart from that, the above form with the JavaScript code that is in the attached file should work fine.

[upl-file uuid=23c27146-4758-410c-b8c8-9d15e42858f0 size=1kB]chadsaun.txt[/upl-file]
Copy linkTweet thisAlerts:
@chadsaunauthorSep 21.2005 — I tried that and it seems like it's not even calling the validate function. It is submitting the form because the action gets carried out, but I even put an alert in the function and no alert showed up. Why would it not be calling the function?
Copy linkTweet thisAlerts:
@David_HarrisonSep 21.2005 — You didn't download the file I attached, I also altered the function a bit. I put a test alert in and it popped up when I submitted.
×

Success!

Help @chadsaun spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...