...

View Full Version : Javascript not working in internet explorer



swj
05-13-2007, 08:09 AM
Hi!

I hope my english isnīt too bad ;)

I have a problem I hope you can help me to solve. I have a code wich works all right in Firefox and Opera, but not in IE. I donīt know why. In Firefox all fields gets controlled, and a alert box is showing if any fields are empty. In IE you can send an empty form widthout control, no alert boxes are shown.

Any suggestions?




script type="text/javascript">
function checkform(){
var complete = true;
contact = document.getElementById('contact');
// sjekker om forname er skrevet inn.
if (contact.name.value == "" || contact.name.value == null){
alert("Vennligst oppgi navn");
contact.name.focus();
complete = false;
}
//sjekker om epost er skrevet inn
else if(contact.email.value == "" || contact.email.value == null){
alert("Vennligst fyll ut din epost adresse");
contact.email.focus();
complete = false;
}
//sjekker at eposten har riktig syntaks
else if (contact.email.value.indexOf("@") == -1){
alert("Du har ikke oppgitt en gyldig epostadresse");
contact.email.focus();
complete = false;
}


//sjekker at subjectfeltet er fylt ut
else if(contact.subject.value == "" || contact.subject.value == null){
alert("Vennligst fyll ut emne");
contact.subject.focus();
complete = false;
}
//sjekker at messagefeltet er fylt ut
else if(contact.message.value == "" || contact.message.value == null){
alert("Vennligst skriv inn beskjeden");
contact.message.focus();
complete = false;
}

return complete;
}

</script>


<form action="mail.php" onsubmit="return checkform();" method="post" enctype="multipart/form-data" id="contact">
<table width="500">
<tr>
<td><font size="2">Navn</font></td>
<td><input name="name" type="text" id="name"/>
</td>
</tr>
<tr>
<td><font size="2" >E-post</font></td>
<td><input name="email" type="text" id="email"/>
</td>
</tr>

<tr>
<td><font size="2" >Emne</font></td>
<td><input name="subject" type="text" id="subject"/></td>
</tr>
<tr>
<td><font size="2" >Beskjed</font></td>
<td><textarea name="message" cols="30" rows="10" id="message"></textarea></td>
</tr>

<tr>
<td></td>
<td ><input name="submit" type="submit" id="submit" value="Send" />
<input name="reset" type="reset" id="reset" value="Nullstill"/> </td>
</tr>

</table>

</form>

_Aerospace_Eng_
05-13-2007, 09:35 AM
Your variable contact can't be the same as the name or id of the form. Change the id of your form to contact1 and it should work. Be sure to change it in document.getElementbyId as well.

Philip M
05-13-2007, 09:39 AM
Delete the line in red

function checkform(){
var complete = true;
contact = document.getElementById('contact');

and add the bit in blue

<form action="mail.php" onsubmit="return checkform();" method="post" enctype="multipart/form-data" name = "contact" id="contact">

That seems to fix it.

Or as AE says, simply give the form a new name and id.

_Aerospace_Eng_
05-13-2007, 09:41 AM
If you do that then its going to error out in Firefox. You will need to change all instances of contact to document.forms['contact'] for it to not error out.

swj
05-13-2007, 09:54 AM
It is a bit embarrassing, but the whole problem was that I had forgot to write "var" before
contact = document.getElementById('contact');

It is working fine now! Thank you very much! I am trying to learn javascript som I am sure I am coming back to you ;)

_Aerospace_Eng_
05-13-2007, 09:58 AM
Hmm I still don't think its good practice to have variables with the same name as your id or name of an element.

swj
05-13-2007, 11:27 AM
Hmm I still don't think its good practice to have variables with the same name as your id or name of an element.


Thank you very much! I will do as you say and change the name.

glenngv
05-14-2007, 07:39 PM
You can avoid getting the reference to the form if you pass this in the form onsubmit handler. And field values will never be null, so you don't have to test for null.

function checkform(frmContact){
var complete = true;
// sjekker om forname er skrevet inn.
if (frmContact.name.value == ""){
alert("Vennligst oppgi navn");
contact.name.focus();
complete = false;
}
...
}

<form action="mail.php" onsubmit="return checkform(this);" method="post" enctype="multipart/form-data" id="contact">



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum