By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,480 Members | 1,229 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,480 IT Pros & Developers. It's quick & easy.

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

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

Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. function IsNumeric(strString)
  3.    //  check for valid numeric strings    
  4.    {
  5.    var strValidChars = "0123456789.-";
  6.    var strChar;
  7.    var blnResult = true;
  8.  
  9.    if (strString.length == 0) return false;
  10.  
  11.    //  test strString consists of valid characters listed above
  12.    for (i = 0; i < strString.length && blnResult == true; i++)
  13.       {
  14.       strChar = strString.charAt(i);
  15.       if (strValidChars.indexOf(strChar) == -1)
  16.          {
  17.          blnResult = false;
  18.          }
  19.       }
  20.    return blnResult;
  21.    }
  22. function valid_form()
  23. {
  24.  
  25.     var emailfilter=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/
  26.     var e=document.getElementById('form').email.value
  27.     var tphone=document.getElementById('form').phone.value;
  28.     var returnval=emailfilter.test(e)
  29.     var cityw=document.getElementById('form').country.value;
  30.     var tname=document.getElementById('form').name.value;
  31.     var query=document.getElementById('form').textfield2.value;
  32.  
  33.     if(query=="")
  34.     {
  35.         alert("Please specify your requirement.")    
  36.         document.getElementById('form').textfield2.focus();
  37.  
  38.         return false;
  39.     }
  40.  
  41.  
  42.     if(tname=="")
  43.     {
  44.         alert("Please enter your name.")
  45.         document.getElementById('form').name.focus();
  46.         return false;
  47.     }
  48.  
  49.  
  50.     if(e=="")
  51.     {
  52.         alert("Please enter your email.")
  53.         document.getElementById('form').email.focus();
  54.         return false;
  55.     }
  56.  
  57.     if(returnval==false)
  58.     {
  59.         alert("Please enter your valid email.")
  60.             document.getElementById('form').email.focus();
  61.         return false;
  62.     }
  63.         if(tphone==false)
  64.     {
  65.         alert("Please enter your phone number.")
  66.         document.getElementById('form').phone.focus();
  67.         return false;
  68.     }
  69.      if (IsNumeric(tphone)==false){
  70.               alert(" Please enter a valid phone number.");
  71. document.getElementById('form').phone.focus();
  72.         return false
  73.     }
  74.  
  75.     if(cityw=="")
  76.     {
  77.         alert("Please select your country.")
  78.         document.getElementById('form').focus();
  79.         return false;
  80.     }
  81.  
  82.  
  83. }
  84.  </script>
Apr 13 '09 #1
Share this Question
Share on Google+
3 Replies


RamananKalirajan
100+
P: 607
You have used "document.getElementById('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
Apr 13 '09 #2

acoder
Expert Mod 15k+
P: 16,027
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.
Apr 13 '09 #3

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


@RamananKalirajan
Apr 16 '09 #4

Post your reply

Sign in to post your reply or Sign up for a free account.