0

使用 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 上运行?代码有什么问题?

4

1 回答 1

0

try this code it is working in firefox also

validation code in javascript for email validation http://informationiswelth.blogspot.in/

于 2013-07-01T14:10:38.363 回答