2010-05-16 28 views
-1

我已经在html页面中添加了一些javascript输入validation.same页面在IE和Chrome中正常工作,但在mozila其不工作。问题是,当用户输入无效数据显示警报味精盒,当用户点击OK它应该返回形成...... mozila不是等着警告框它只是显示警告框5-6秒,然后进入到下一页中定义的form action =“nextpage.php”java脚本不工作在mozila

function validate_form(thisform) 
    { 
    with (thisform) 
     { 
     if (validate_required(oldpassword, "<b>Error: </b>Please enter the Old Password!") == false) 
     { changeColor("oldpassword"); return false; } 

     else if (valid_length(newpassword, "<b>Error: </b>Please enter the New Password!!") == false) 
     {newpassword.value=""; changeColor("newpassword"); return false; } 

     else if (valid_length(cnfpassword, "<b>Error: </b>Please enter the Confirm Password!!") == false) 
     {cnfpassword.value=""; changeColor("cnfpassword"); return false; } 

     else if (document.getElementById('newpassword').value != document.getElementById('cnfpassword').value) 
     {changeColor("newpassword");cool.error("<b>Error: </b>Passwords entered are not same!"); 
     newpassword.value="";cnfpassword.value="";return false;} 

    } 
}function validate_required(field, alerttxt) 
    { 
    with (field) 
     { 
     if (value == null || value == "") 
      { 
      cool.error(alerttxt);return false; 
      } 
     else 
      { 
      return true; 
      } 
     } 
    } 

cool.error无非是CSS次的js警报box.I事情没有我代码天气问题的任何问题在一些浏览器settings.Is会这样???因为它工作正常IEChrome

+3

作为一般说明在这里,不要假设错误是**不在你的代码**,99.9999%的时间,至少在已建立的平台上,**它是你的代码**,假设相反将不会做任何事情但浪费时间找到原因。 – 2010-05-16 19:56:55

+0

作为另一个不是你可能想看看一些jQuery验证插件可能会节省一些时间 – mcgrailm 2010-05-16 20:00:11

回答

4

您正在使用非标准的仅限IE的行为,它为每个带有ID的元素创建全局变量。

为了解决这个问题,添加一个全局变量使用的每个元素:

var oldpassword = document.getElementById("oldpassword"); 

此外,你应该从未使用JavaScript的with块。
它与VB的with区块截然不同,除非您真正理解其缺陷和缺陷,否则不应使用。它也会减慢你的代码。

+1

对不起,我没有得到你...你可以解释更多... – nectar 2010-05-16 19:57:03

+0

添加行,如'var oldpassword = document.getElementById(“ oldpassword“);'在Javascript中使用的每个HTML元素的函数之外。 – SLaks 2010-05-16 20:01:25

+0

或者'myformobject.elements.oldpassword',如果你只使用名字。但使用ID更容易。 – bobince 2010-05-16 20:11:27

0

您也可以使用一些JavaScript库来获取浏​​览器问题。我生根于jQuery。

+1

可以用jquery作为javascript进行验证吗? – nectar 2010-05-16 20:28:17