JavaScript Prompts Are Not Prompting in FFX
This is my first time posting (newbie here/just started to program), hope someone can help.
I am working on a simple form which consists of four fields (first name, last name, email and a comment box). The JavaScript (as follows) works accordingly by prompting a box telling the user to fill in all fields if they are not all completed...in IE. However, this is not the case in FFX. In FFX, I am able to submit the form w/ or w/out any of the fields filled out.
Note: a simple Access database is set up on a sever and processes the asp.form - information is collected, in FFX and in IE w/out any hiccups.
Here's the code (note, I omitted the comment.value since it was breaking the rest of the page).... Thanks in advance.
blkx
JS code:
function validateContactUs()
{
if ((contactus.firstname.value == "") || (contactus.lastname.value == "") || (contactus.email.value == ""))
{
alert("All fields are required!");
return false;
}
return true;
}
Re: JavaScript Prompts Are Not Prompting in FFX
How are you calling your function?
Re: JavaScript Prompts Are Not Prompting in FFX
You're using global namespace of forms in IE, which is a specific feature of that browser. Thus your code is unlikely to work in any browser that isn't IE.
First of all, in the contactus form, make sure you've given that name using ID attribute and not the name attribute. Add ID fields also for the other form fields (for those you don't need to remove name attribute from them, it will be used for submitting the form). Next, you change your code to look like this:
Code:
function validateContactUs() {
var contactus = document.getElementById('contactus');
// check there is an element of that ID
if(contactus) {
var firstname = contactus.getElementById('firstname');
var lastname = contactus.getElementById('lastname');
var email = contactus.getElementById('email');
if ((firstname.value == "") || (lastname.value == "") || (email.value == "")) {
alert("All fields are required!");
return false;
}
return true;
}
You might want to read up about W3C standards so that you don't end up using IE proprietary features, so you'll ensure that your sites work with all the browsers. For example, if you happen to have document.all in your code, that is something not really supported outside IE. Sometimes you'll have to do separate code for IE and the other browsers.
The browser world is split to mainly two sides: the standard aware modern feature rich browsers, and often Microsoft proprierary feature rich IE.
Besides IE and Firefox, download Safari for Windows and Opera. These give you more understanding of what generally works and what doesn't.
Also, in Firefox, you can debug using Tools > Error console. Firebug and Developer Toolbar are also great extensions to have.
Re: JavaScript Prompts Are Not Prompting in FFX
you also have no else. If you don't include an else before you return true, then the function will always return true after it returns false.
Re: JavaScript Prompts Are Not Prompting in FFX
Quote:
Originally Posted by MarkT
How are you calling your function?
<script type="text/javascript" src="validateContactUs.js"></script>
blkx
Re: JavaScript Prompts Are Not Prompting in FFX
Quote:
Originally Posted by blkx
<script type="text/javascript" src="validateContactUs.js"></script>
blkx
sorry, I tried editing the post as I hit the submit button a bit too quickly...
// {
// alert("All fields are required!");
// return false;
// }
Re: JavaScript Prompts Are Not Prompting in FFX
Quote:
Originally Posted by JPnyc
you also have no else. If you don't include an else before you return true, then the function will always return true after it returns false.
My apologies for newbie-ness. But, if I understand correctly, I would need an 'else' before a 'return true' vs after 'return true'.
blkx
Re: JavaScript Prompts Are Not Prompting in FFX
Quote:
Originally Posted by Merri
You're using global namespace of forms in IE, which is a specific feature of that browser. Thus your code is unlikely to work in any browser that isn't IE.
*snip snip*
Thank you (all) - I'll try this out and report back. Very much appreciate everyone's times and responses.
blkx
Re: JavaScript Prompts Are Not Prompting in FFX
Yes you need to have:
else {
return true;
}
if you just include a line of code after an if , without the else clause, it will just execute no matter what the results of the above test were.
Re: JavaScript Prompts Are Not Prompting in FFX
Quote:
Originally Posted by JPnyc
Yes you need to have:
else {
return true;
}
if you just include a line of code after an if , without the else clause, it will just execute no matter what the results of the above test were.
Thank you for clarification; much appreciated. After the post was submitted, a little light went off (thank you everyone, again)
blkx
Re: JavaScript Prompts Are Not Prompting in FFX
JPnyc: incorrect. Return always exists the function when it is called, be it within an if clause, or whatever else. When a return is met the function will be exited.
A simple test:
Code:
<html>
<title>JavaScript return test</title>
<script type="text/javascript">
function test() {
alert("Will the code run after return?");
if(true) {
return true;
}
alert("Am I executed?");
}
</script>
<h1>Test</h1>
<p><a href="#" onclick="test();">How does <code>return</code> work?</a></p>
</html>
You never get the second alert box. So, there is nothing wrong with blkx's original code on that part.
Re: JavaScript Prompts Are Not Prompting in FFX
Ironically, this is what ended up working (it wanted "document."):
function validateContactUs()
{
var cs
cs = document.contactus;
if ((cs.firstname.value == "") || (cs.lastname.value == "") || (cs.email.value == ""))
{
alert("All fields are required!");
return false;
}
return true;
}
Thank you all for your help!
blkx