Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    swj
    swj is offline
    New to the CF scene
    Join Date
    May 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript not working in internet explorer

    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?



    Code:
    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>
    Code:
    <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>
    Last edited by swj; 05-13-2007 at 08:14 AM.

  • #2
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,982
    Thanks
    203
    Thanked 2,536 Times in 2,514 Posts
    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.
    Last edited by Philip M; 05-13-2007 at 09:42 AM.

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #5
    swj
    swj is offline
    New to the CF scene
    Join Date
    May 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It is a bit embarrassing, but the whole problem was that I had forgot to write "var" before
    Code:
    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

  • #6
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    Hmm I still don't think its good practice to have variables with the same name as your id or name of an element.
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #7
    swj
    swj is offline
    New to the CF scene
    Join Date
    May 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by _Aerospace_Eng_ View Post
    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.

  • #8
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    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.
    Code:
    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;
    	}
    	...
    }
    Code:
    <form action="mail.php" onsubmit="return checkform(this);" method="post" enctype="multipart/form-data" id="contact">


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •