/    Sign up×
Community /Pin to ProfileBookmark

Works in IE but not in Mozilla FireFox

I use a simple form that is on an HTML page that also has some JavaScript functions on it. On the (method=post) form, I use
onSubmit=”return validate()” to validate the user-entered email address. I also use the following action parameter: action=”cfa.asp?todo=1&foundyact=strval.asp&foundnact=strsign.asp&emailad=”>

However, my validation function changes my “action” by appending the entered email address to the initial “action” value so that by the time the subsequent page gets invoked, I have the query string information that I need in place.

The problem is that this all works fine in IE but not in Mozilla FireFox. Can you help to figure that out?

Another interesting fact is that when I run in Mozilla FireFox, it seems to me that the entire “validate” function is not being invoked (because even when I enter an improperly formatted email address I do not get any alert messages… I instead go straight to the CFA page…)

Thank you in advance.

Here is my validate function……

function validate() {
if (! isValidEmail(document.forms[0].frmFrom.value)) {
document.forms[0].hiddenfield.value = “BAD”;
alert(“Please enter a valid email address”);
return false;
}
var action = document.getElementById(“formxd”).action;
var newaction = action + document.forms[0].frmFrom.value;
document.getElementById(“formxd”).action = newaction;
document.getElementById(“formxd”).submit();
document.forms[0].hiddenfield.value = “GOOD”;
alert(“Okay – I’ll look up the info…”);
return true;
}

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@KorDec 29.2004 — try replacing the empty space

if ([color=red]!i[/color]sValidEmail(document.forms[0].frmFrom.value))

I presume you have another fuction isValidEmail()...

but wouldn't be easier to use a single function to validate?
Copy linkTweet thisAlerts:
@swoop80authorDec 29.2004 — Thanks Kor. Interestingly, that did help. Now, when I enter an email with impropper format, I get the expected alert message. However, when I enter a good email address, the expected "action" manipulation and alert message for that does not happen... I just go straight off to CFA...

I got to thinking that perhaps the contents of the called function (isValidEmail) could be an issue...

I noted that you suggested that it should perhaps not be a separate one. I'd like to leave it separate for now if it'll work that way. See, it's in it's own htm file and I won't always want to do the same other processing (alerts and action swapping, etc) when validating an email.

Here are the contents of that isValidEmail htm file... in case you have a moment to try to figure out more about my Mozilla problems...

Again - thank you so much for your time, Kor.

function isValidEmail(email, required) {

if (required==undefined) { // if not specified, assume it's required

required=true;

}

if (email==null) {

if (required) {

return false;

}

return true;

}

if (email.length==0) {


if (required) {

return false;

}

return true;

}

if (! allValidChars(email)) { // check to make sure all characters are valid

return false;

}

if (email.indexOf("@") < 1) { // must contain @, and it must not be the first character

return false;

} else if (email.lastIndexOf(".") <= email.indexOf("@")) { // last dot must be after the @

return false;

} else if (email.indexOf("@") == email.length) { // @ must not be the last character

return false;

}

return true;
}

function allValidChars(email) {

var parsed = true;

var validchars = "[email protected]";

for (var i=0; i < email.length; i++) {

var letter = email.charAt(i).toLowerCase();

if (validchars.indexOf(letter) != -1)

continue;

parsed = false;

break;

}

return parsed;

}
Copy linkTweet thisAlerts:
@JPnycDec 29.2004 — I don't see any alert for the return true conditions. Can you point it out?
Copy linkTweet thisAlerts:
@swoop80authorDec 29.2004 — Sure thing. The alert I was speaking of is in the first (primary) function that is called (not in the "isValidEmail" function that is also now included above)... Please see my first post above.

Here is the alert that I'm not getting when using FireFox and when I enter a validly formatted email address:

alert("Okay - I'll look up the info...");
Copy linkTweet thisAlerts:
@NedalsDec 29.2004 — [size=1]
document.getElementById("formxd").submit();
document.forms[0].hiddenfield.value = "GOOD";
alert("Okay - I'll look up the info...");
[/size]
You are submitting the document before the alert. Move the hiddenfield and alert lines above the submit line.
Copy linkTweet thisAlerts:
@swoop80authorDec 30.2004 — Thank you. Actually, I think that "submit" shouldn't be needed (and appears that it really isn't in IE) since the form action is done only after the true condition is achieved.

However, in Mozilla FireFox, neither of the below "validate" functions works either.

Another Note: Based on above discussions, I went and changed the sub-function that is called by my separate email validator (the email validator is "isValidEmail" and it's subfunction is called "allValidChars") since the invocation of that sub-function also had a space in the spot referenced by Kor... but my removing that space did not help.

function validate() {

if (!isValidEmail(document.forms[0].frmFrom.value)){

document.forms[0].hiddenfield.value="BAD";

alert("Please enter a valid email address");

return false;

}

else

var action=document.getElementById("formxd").action;

var newaction=action+document.forms[0].frmFrom.value;

document.getElementById("formxd").action=newaction;

document.forms[0].hiddenfield.value="GOOD";

alert("Okay - I'll look up the info...");

document.getElementById("formxd").submit();

return true;

}



(THE BELOW ALSO DOESN'T WORK IN FIREFOX... THIS VERSION HAS NO SUBMIT)

function validate() {

if (!isValidEmail(document.forms[0].frmFrom.value)){

document.forms[0].hiddenfield.value="BAD";

alert("Please enter a valid email address");

return false;

}

else

var action=document.getElementById("formxd").action;

var newaction=action+document.forms[0].frmFrom.value;

document.getElementById("formxd").action=newaction;

document.forms[0].hiddenfield.value="GOOD";

alert("Okay - I'll look up the info...");

return true;

}
Copy linkTweet thisAlerts:
@swoop80authorDec 30.2004 — Additional Note: On the Mozilla website, I found the "venkman" script debugger that is used with FireFox and tried it out in hopes of debugging this problem.

The message I got back says that my main validator function (called "validate") isn't defined... I'm more bewildered now than ever.

Here's the portion of the page where the form is defined in way of reference... as always, your help is much appreciated.


<div id="Layer5" style="position:absolute; width:371px; height:379px; z-index:3; left: 316px; top: 151px; background-color: #660000; layer-background-color: #660000; border: 1px none #000000;">

<td height="300" colspan="2"><form name="formxd" method="post" onSubmit="return validate()" action="cfa.asp?todo=1&foundyact=strval.asp&foundnact=strsign.asp&emailad=">

<table width="367" border="0" align="center" bgcolor="#660000">

<tr>

<td colspan="3"><div align="center"><font face="Verdana, Arial, Helvetica, sans-serif">

</font></div></td>

</tr>

<tr>

<td colspan="3"><div align="center"><font face="Verdana, Arial, Helvetica, sans-serif"><strong>Please

enter your email address to log in</strong></font></div></td>

</tr>

<tr>

<td width="118" valign="top"><div align="right"></div></td>

<td colspan="2"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">

<input name="frmFrom" type="text" id="frmFrom">

</font></td>

</tr>

<tr>

<td><div align="right"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font></div></td>

<td colspan="2"> <div align="left"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">

<input type="hidden" name="hiddenfield" value="b.l.a.n.k.">

</font></div></td>

</tr>

<tr>

<td><div align="right"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font></div></td>

<td width="160"><div align="center"> <font size="2" face="Verdana, Arial, Helvetica, sans-serif">

<input type="submit" name="Submit" value="Submit">

</font></div></td>

<td width="81">&nbsp;</td>

</tr>

<tr>

<td><div align="right"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font></div></td>

<td colspan="2"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font></td>

</tr>

<tr>

<td><div align="right"><font size="2">&nbsp;</font></div></td>

<td><div align="center"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">To

log out, click here.</font></div></td>

<td>&nbsp;</td>

</tr>

<tr>

<td height="55"> <div align="right"></div></td>

<td colspan="2">&nbsp;</td>

</tr>

</table>

</form></td>
Copy linkTweet thisAlerts:
@swoop80authorDec 30.2004 — Thank you all for your help.

This has been solved.

I found that IE is "working" more or less as a fluke. See, a "form" can have a "name" specified at definition time or it can have an "id" specified (or it can even have both).

Mine had a "name" specified... so, I really shouldn't have been able to get "getElementById" to work (in any browser). ("getElementByName" would have worked okay... except that this isn't available in Mozilla).

Solution: just change my form to have an "id" instead of a "name".

It now works in Mozilla Firefox and in IE.

Hope that someone else can learn from this.

Thanks again for your help.
Copy linkTweet thisAlerts:
@KorJan 03.2005 — 
("getElementByName" would have worked okay... except that this isn't available in Mozilla).
[/quote]


Oh, but it works in Mozilla also... Except that the method is getElement[color=red]s[/color]ByName(), similar with getElementsByTagName(), thus you must specify the index either

getElementsByName('thename')[0]
Copy linkTweet thisAlerts:
@swoop80authorJan 04.2005 — Thank you, Kor. This stuff is very interesting (as you can tell, I have a lot to learn, still).

Here is what my "validate" routine has become:

function validate(){

alert("about to validate");

if (!isValidEmail(document.forms[0].stueemail.value)){

document.forms[0].hiddenfield.value="BAD";

alert("Please enter a valid email address");

return false;

}

else

var form1msg = "at least one form field is invalid";

if(!Form1OK(form1msg)){

document.forms[0].hiddenfield.value="BAD";

alert(form1msg);

return false;

}

else

alert("we think we are okay now");

var action=document.getElementById("form1").action;

var newaction=action+document.forms[0].stueemail.value;

document.getElementById("form1").action=newaction;

document.forms[0].hiddenfield.value="GOOD";

alert("Okay - I'll update the file...");

return true;

}


Kor - as you can see, I have now come up with the need for another function to be called from this validation routine. The name of the

new routine is "Form1OK". I am trying to use this new routine (has

extension type of ".js") to confirm contents of all the fields on my input form (other than email address - that one is taken care of by isValidEmail).

Anyway, my newest problem is as follows:

Form1Ok is being executed as expected. When I enter a different

value in field stucemail from that of stueemal, I do receive the

alert message that says "not equal". However, instead of taking the "false" path when I come back to the "validate" routine, it seems

that upon reentry to the main program, I go straight off to my

"form action page"... without alert above that says "we think we are okay now" or the alert that is to print the contents of form1msg (which is set within Form1OK).

I am fairly convinced that I've made some syntactical error, but being new to this, my error is not evident to me. Any assistance finding it would be appreciated.

Here is the contents of file Form1OK.js:

function Form1OK(msg, required) {

alert(document.forms[0].stueemail.value);

if (required==undefined) { // if not specified, assume it's required

required=true;

}

if (document.forms[0].stueemail.value==null) {

msg = "required field not entered - email address";

return false;

} else alert(document.forms[0].stucemail.value);

if (document.forms[0].stueemail.value != document.forms[0].stucemail.value) {

alert("not equal");

msg = "confirmation email not equal entered email";

return false;

}

alert("doing true");

return true;

}
Copy linkTweet thisAlerts:
@swoop80authorJan 04.2005 — Please hold the phone...

Hope you are seeing this before spending a lot of time on the prior post. I've found some syntax issues with what I had.... Also, back in the main "validate" function, I was trying to update a hidden field

that I simply did not have defined in my form... a holdover from a copied page. Apparently, that sort of issue really confuses JavaScript.

Anyway, I've cleaned it up now and it is working.

Hope I haven't wasted anyone's time.

Thank you.
×

Success!

Help @swoop80 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 5.18,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

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

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...