【问题标题】:java script form validation works fine in chrome but not working in firefox?java脚本表单验证在chrome中工作正常但在firefox中不起作用?
【发布时间】:2013-06-17 11:41:17
【问题描述】:

使用 java 脚本进行表单验证在 chrome 上运行良好。但不能在 Firefox 中工作。

代码:

<script type="text/javascript">
function validateForm()
{
var x=document.forms["newuserForm"]["mname"].value;
if (x==null || x=="")
  {
  alert("name must be filled");
  return false;
  }

  var x=document.forms["newuserForm"]["email"].value;
var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");
if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert(" email is not valid");
  return false;
  }



  var x=document.forms["newuserForm"]["password"].value;
if (x==null || x=="")
  {
  alert("password must be filled out ");
  return false;
  }

/*  var x=document.forms["newuserForm"]["age"].value;
if (x==null || x=="")
  {
  alert("Age must be Selected");
  return false;
  }*/
var m = document.getElementById('male');
var f = document.getElementById('female');

if ( (m.checked == false ) && (f.checked == false ) )
{
alert ( "Please select your gender ");
return false;
}
if( document.newuserForm.age.value == "-1" )
   {
     alert( "please select your age group" );
     return false;
   }

if( document.newuserForm.country.value == "-1" )
   {
     alert( "Please select your country");
     return false;
   }

if( document.newuserForm.city.value == "-1" )
   {
     alert( "please select your country" );
     return false;
   }

}
</script>

在 chrome 中如果用户不输入姓名,它会显示一条消息“姓名必须填写”。但在 Firefox 中没有任何显示,如果我单击提交按钮而不输入名称,它将注册用户。为什么 java 脚本不能在 Firefox 上运行?代码有什么问题?

【问题讨论】:

  • 试试这个 var x=document.forms[0].mname.value;
  • 'Java' 之于 'JavaScript' 就像 'Car' 之于 'Carpet'。
  • “它将注册用户”意味着您没有检查客户端发送到服务器的内容。始终检查服务器端。永远不要相信客户
  • 可以分享html代码吗?
  • 请注意return false; 是不够的。阅读e.preventDefault();e.stopPropagation();

标签: javascript forms


【解决方案1】:

试试这个代码,它也可以在 Firefox 中运行

用于电子邮件验证的 javascript 中的验证代码 http://informationiswelth.blogspot.in/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-09-26
    • 2016-07-13
    • 1970-01-01
    • 2020-07-28
    • 1970-01-01
    • 2013-12-13
    • 2016-10-06
    • 1970-01-01
    相关资源
    最近更新 更多