javascript form validation code works in IE but not in Mozilla-Firefox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PrabodhanP
    New Member
    • Apr 2009
    • 14

    javascript form validation code works in IE but not in Mozilla-Firefox

    I hv following javascript form validation code works in IE but not in Mozilla-Firefox ...please suggest

    Code:
    <script type="text/javascript">
    function IsNumeric(strString)
       //  check for valid numeric strings	
       {
       var strValidChars = "0123456789.-";
       var strChar;
       var blnResult = true;
    
       if (strString.length == 0) return false;
    
       //  test strString consists of valid characters listed above
       for (i = 0; i < strString.length && blnResult == true; i++)
          {
          strChar = strString.charAt(i);
          if (strValidChars.indexOf(strChar) == -1)
             {
             blnResult = false;
             }
          }
       return blnResult;
       }
    function valid_form()
    {
    		
    	var emailfilter=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/
    	var e=document.getElementById('form').email.value
    	var tphone=document.getElementById('form').phone.value;
    	var returnval=emailfilter.test(e)
    	var cityw=document.getElementById('form').country.value;
    	var tname=document.getElementById('form').name.value;
    	var query=document.getElementById('form').textfield2.value;
    	
    	if(query=="")
    	{
    		alert("Please specify your requirement.")	
    		document.getElementById('form').textfield2.focus();
    		
    		return false;
    	}
    	
    	
    	if(tname=="")
    	{
    		alert("Please enter your name.")
    		document.getElementById('form').name.focus();
    		return false;
    	}
    	
    	
    	if(e=="")
    	{
    		alert("Please enter your email.")
    		document.getElementById('form').email.focus();
    		return false;
    	}
    	
    	if(returnval==false)
    	{
    		alert("Please enter your valid email.")
    			document.getElementById('form').email.focus();
    		return false;
    	}
    		if(tphone==false)
    	{
    		alert("Please enter your phone number.")
    		document.getElementById('form').phone.focus();
    		return false;
    	}
    	 if (IsNumeric(tphone)==false){
    		      alert(" Please enter a valid phone number.");
    document.getElementById('form').phone.focus();
    		return false
    	}
    	
    	if(cityw=="")
    	{
    		alert("Please select your country.")
    		document.getElementById('form').focus();
    		return false;
    	}
    	
    	
    }
     </script>
    Last edited by Dormilich; Apr 13 '09, 07:27 AM. Reason: added [code] tags
  • RamananKalirajan
    Contributor
    • Mar 2008
    • 608

    #2
    You have used "document.getEl ementById('form ')". Just check this one.. Have you given an id attribute to the form ie. <form name="form" id="form">. In IE if u have given name it will automatically take as ID, but mozilla wont take it.. Just Check that one.. I havent checked ur js code.. This is the common mistake i would do when i started working on scripts. If this is not ur problem, post the html code also...

    Regards
    Ramanan Kalirajan

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      It's easy to avoid that mistake by never testing on IE first unless you want to develop bad coding habits and plenty of headaches later.

      OP: your isNumeric function needs a bit of adjustment. It allows multiple - and . which would not be a valid numeric value.

      Comment

      • PrabodhanP
        New Member
        • Apr 2009
        • 14

        #4
        thanks

        Thank you ,very much sir for quick reply...it works fine now


        Originally posted by RamananKaliraja n
        You have used "document.getEl ementById('form ')". Just check this one.. Have you given an id attribute to the form ie. <form name="form" id="form">. In IE if u have given name it will automatically take as ID, but mozilla wont take it.. Just Check that one.. I havent checked ur js code.. This is the common mistake i would do when i started working on scripts. If this is not ur problem, post the html code also...

        Regards
        Ramanan Kalirajan

        Comment

        Working...