| | |
Alert box, then cancel action (Javascript)
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved
![]() |
•
•
Join Date: Nov 2006
Posts: 26
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Dec 2004
Posts: 1,655
Reputation:
Solved Threads: 35
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".
![]() |
Similar Threads
- how can i make php alert pop ups just like in javascript. (PHP)
- passing values of Select box from JSP to Action class thru form Bean (JSP)
- Can I get an alert box on my local computer when someone access my web page? (ASP.NET)
- form validation not working (JavaScript / DHTML / AJAX)
- JavaScript's window.opener (JavaScript / DHTML / AJAX)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: Selectively Printing only Certain Divs (javascript/css question)
- Next Thread: Can't put 2 Javasript working in the same page!
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate automatically beta box browser bug captchaformproblem checkbox close codes createrange() css cursor debugger decimal dependent disablefirebug dom download dropdown editor element engine error events explorer ext file firefox form forms frameworks getselection google gwt gxt hiddenvalue highlightedword hint html htmlform ie7 ie8 iframe index internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jsp jump listbox maps masterpage math media microsoft mp4 object onmouseoutdivproblem onreadystatechange paypal pdf php player position programming progressbar prototype redirect regex runtime safari scale scriptlets search security select size software sql text textarea unicode w3c window windowofwords windowsxp wysiwyg \n






