javascript表单验证在IE8中不起作用

时间:2012-12-10 06:49:22

标签: javascript forms internet-explorer validation onsubmit

这个javascript表单验证在IE8中不起作用!! 表单在用户输入无效输入时提交。 它在其他浏览器中正常工作firefox,opera,chrome。 你可以帮帮我吗?

<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>

1 个答案:

答案 0 :(得分:2)

我猜测问题可能是因为检查函数中字符串的.trim()方法。在旧浏览器中没有String trim方法,无论浏览器如何,您都必须使用polyfill来确保它始终可用。一个很好的例子是:

Trim string in JavaScript?

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

是我使用的。