944,120 Members | Top Members by Rank

Ad:
Nov 17th, 2006
0

Alert box, then cancel action (Javascript)

Expand Post »
Hi,

I'm trying to write a form that has required fields. When the form is filled out with the required fields missing, I'm trying to have a pop-up that tells them what is missing and then cancels the submission of the form.

At the moment I have the function below linked into the the Submit button like so: <submit onClick="validateForm()">

[html]function validateForm(){
var form_object = document.forms.helpform;
if(form_object.elements.Name.value == ''){
alert('Please enter your name.');
return false;
} else if(form_object.elements.Email.value == ''){
alert('Please enter your email address');
return false;
} else if(form_object.elements.Mobile.value == ''){
alert('Please enter your mobile phone number');
return false;
} else if(form_object.elements.Description.value == ''){
alert('Please enter a description of what you would like us to help you with.');
return false;
}
return true;
}[/html]


I have the function in the the head of the document.

The pop-ups work fine, but it doesn't cancel the form from submitting. If anyone has any suggestions, they would be most appreciated

Thanks in Advance
Last edited by tgreer; Nov 19th, 2006 at 9:17 pm. Reason: added code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
ForestTech is offline Offline
26 posts
since Nov 2006
Nov 17th, 2006
0

Re: Alert box, then cancel action (Javascript)

use this code
Quote ...
<form onSubmit="return validateForm()">
Last edited by vishesh; Nov 17th, 2006 at 4:28 am.
Reputation Points: 85
Solved Threads: 42
Nearly a Posting Virtuoso
vishesh is offline Offline
1,362 posts
since Oct 2006
Nov 19th, 2006
0

Re: Alert box, then cancel action (Javascript)

That didn't seem to work. The code works, except that it doesn't stop the form submission process. Does anyone know of any code that would stop the current action or anything like that. I know a little bit of javascript, but I'm still on the learning curve.
Reputation Points: 10
Solved Threads: 0
Light Poster
ForestTech is offline Offline
26 posts
since Nov 2006
Nov 19th, 2006
0

Re: Alert box, then cancel action (Javascript)

That should work. You need to return either true or false, and call the function using the "return" keyword as vishesh has shown. He's missing the terminal semicolon, but it should still work. Also, you can't code both an "onsubmit" for the form AND "onclick" on the submit button. Your code would run twice. Pick a single event handler, use the "return" keyword, and make sure each code path in your function returns either "true" or "false".
Team Colleague
Reputation Points: 227
Solved Threads: 37
Made Her Cry
tgreer is offline Offline
1,697 posts
since Dec 2004
Nov 20th, 2006
0

Re: Alert box, then cancel action (Javascript)

Problem solved. Thanks guys
Reputation Points: 10
Solved Threads: 0
Light Poster
ForestTech is offline Offline
26 posts
since Nov 2006
Jul 19th, 2010
0
Re: Alert box, then cancel action (Javascript)
Brilliant. This is just what I needed. Thanks guys. All I needed was a return in my onClick.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kamikaze63 is offline Offline
1 posts
since Mar 2010

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: Posting Form Elements through AJAX
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: [Prototype] Validating a form before sending it via Ajax





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC