使用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中什么也不会显示,如果我点击submit按钮而没有输入名称,它将注册用户。为什么java脚本不能在Firefox上工作?代码有什么问题?
发布于 2013-07-01 22:10:38
试试这段代码,它在firefox中也是有效的
电子邮件验证http://informationiswelth.blogspot.in/的javascript中的验证代码
https://stackoverflow.com/questions/17146648
复制相似问题