2012-12-10 57 views
1

此javascript表單驗證在IE8中不起作用! 表單提交時用戶間無效輸入。 它在其他瀏覽器firefox,opera,chrome中正常工作。 你能幫我嗎?JavaScript表單驗證在IE8中不起作用

<form method="post" name="form1" id="form1" action="editAction.php" onsubmit="return check_user_info()"> 

===============================

<script type="text/javascript"> 

    function check_user_info() 
    { 
    var proceed = true; 
     if (checkUserFirstName() == 1) 
     { 
     document.getElementById("userFname_msg").style.display = "block"; 
     document.getElementById("userFname_msg1").style.display = "none"; 
     proceed = false; 
     } 
     else if (checkUserFirstName() == 11) 
      { 
      document.getElementById("userFname_msg1").style.display = "block"; 
      document.getElementById("userFname_msg").style.display = "none"; 
      proceed = false; 
      } 
      else 
      { 
      document.getElementById("userFname_msg").style.display = "none"; 
      document.getElementById("userFname_msg1").style.display = "none"; 
      } 
      //----------------------------- 
      if (checkUserLastName() == 1) 
     {document.getElementById("userLname_msg").style.display = "block"; 
     document.getElementById("userLname_msg1").style.display = "none"; 
     proceed = false;} 
     else if (checkUserLastName() == 11) 
      {document.getElementById("userLname_msg1").style.display = "block"; 
      document.getElementById("userLname_msg").style.display = "none"; 
      proceed = false;} 

     else{document.getElementById("userLname_msg").style.display = "none"; 
      document.getElementById("userLname_msg1").style.display = "none"; 
      } 
      //----------------------------- 

      if (checkMobile() == 1) 
     { 
     document.getElementById("mobile_msg").style.display = "block"; 
     proceed = false; 
     } 
     else 
     document.getElementById("mobile_msg").style.display = "none"; 


      //---------------------------------- 
       if (checkPhone() == 1) 
     { 
     document.getElementById("phone_msg").style.display = "block"; 
     proceed = false; 
     } 
     else 
     document.getElementById("phone_msg").style.display = "none"; 

      //---------------------------- 
      if (proceed) 
      { 
      alert ("your information has been updated successfully .."); 
      return proceed; 
      } 
      else 
      { 
      return false; 
      } 
     } // End function ... 
    //---------------------------------------------------- 
     function checkUserFirstName() 
     { 
     if (document.getElementById("firstName").value.trim().length == 0) 
     return 1 ; 
     else if (!(document.getElementById("firstName").value.match(/^[ \.\-_a-zA-Z]+$/))) 
     return 11 ; 
     } // End function ... 
     //-------------------------------------------------- 
     function checkUserLastName() 
     { 
     if (document.getElementById("lastName").value.trim().length == 0) 
     return 1 ; 
     else if (!document.getElementById("lastName").value.match(/^[ \.\-_a-zA-Z]+$/)) 
     return 11 ; 
     } // End function ... 
    //------------------------------------------------------------- 
     function checkMobile() 
    { 
    if (((document.getElementById("mobile").value.trim().length >0) && (document.getElementById("mobile").value.trim().length != 10))|| (isNaN(document.getElementById("mobile").value))) 
    return 1 ; 

    } // end function ... 
    //---------------------------------------------------------- 
     function checkPhone() 
    { 
    if(!document.getElementById("TelephoneNumber").value.match(/^[ \/0-9]*$/)) 
    return 1 ; 
    } // end function ... 
    </script> 
+0

可能是因爲你的'check'函數中的字符串有'.trim()'方法。在舊瀏覽器中沒有全局修剪方法,您必須實現自己的設置以確保它始終可用 – Ian

+0

http://stackoverflow.com/questions/498970/how-do-i-trim-a-string -in-javascript – Ian

+0

謝謝..它現在正在工作:) – code

回答

2

我猜問題可能是因爲檢查函數中字符串的.trim()方法。在舊版瀏覽器中沒有字符串trim,您不得不使用polyfill來確保它始終可用,無論瀏覽器如何。一個很好的例子是:

Trim string in JavaScript?

"".trim || String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g, '');}; 

爲我所用。