JavaScript总是返回true
JavaScript always returns true
我将这个脚本添加到jsp文件中。
这个函数总是返回true,即使在没有匹配表达式等失败的情况下。
为什么会这样?如何解决这个问题?
<script type="text/javascript">
function validate()
{
document.getElementById("user").innerHTML="";
document.getElementById("pass").innerHTML="";
document.getElementById("mail").innerHTML="";
document.getElementById("number").innerHTML="";
var expression = /^[a-zA-Z_.0-9]+$/;
var mailExp = /^['w'-'.'+]+'@[a-zA-Z0-9'.'-]+'.[a-zA-z0-9]{2,4}+$/;
var numberExp = /^[0-9]+$/;
if(!document.getElementById('username').value.match(expression))
{
document.getElementById('username').focus();
document.getElementById("user").innerHTML=" Enter valid user name";
return false;
}
if(document.getElementById('password').value.length == 0)
{
document.getElementById('password').focus();
document.getElementById("pass").innerHTML = "Fill Password field";
document.getElementById("pass").style.visibility = "visible";
return false;
}
if(document.getElementById('cnfpassword').value.length == 0)
{
document.getElementById('cnfpassword').focus();
document.getElementById("pass").innerHTML = "Fill confirm Password field";
document.getElementById("pass").style.visibility = "visible";
return false;
}
if(document.getElementById('password').value != document.getElementById('cnfpassword').value)
{
document.getElementById('password').focus();
document.getElementById("pass").innerHTML = "Password and confirm password Not match";
document.getElementById("pass").style.visibility = "visible";
return false;
}
if(!document.getElementById('emailid').value.match(expression))
{
document.getElementById('emailid').focus();
document.getElementById("mail").innerHTML=" Enter valid E-mailID";
document.getElementById("mail").style.visibility = "visible";
return false;
}
if(!document.getElementById('number').value.match(numberExp))
{
document.getElementById('number').focus();
document.getElementById("number").innerHTML=" Enter only numbers";
document.getElementById("number").style.visibility = "visible";
return false;
}
else
{
return true;
}
}
除了重构建议之外,最简单的方法就是使用Firebug并调试出错的地方。这会帮你省去很多麻烦
改变document.getElementById('password').value.length == 0
document.getElementById('password').value.length === 0
0==""
0== "0"
false== "0"
null== undefined
0==' 't'r'n '
解决方案
0===""
0=== "0"
false=== "0"
null=== undefined
0===' 't'r'n '
"==" and "!操作符将在进行比较之前尝试将将两个值强制转换为相同的类型。
"===" and "!操作符(也称为标识操作符)在比较时检查类型和值
以上所说的都是正确的,else只对应于最后一个if
还要注意==和===不一样。
另外,最好在函数中只使用一个return
语句。
相关文章:
- 对于loop.if-仅在经过所有间隔后返回true
- 如果函数返回True,则显示Javascript按钮
- Javascript If else 只返回 TRUE 或只返回 FALSE
- 过滤器返回true或false
- 我对“;返回true"嵌套函数内部;t工作
- 如何在成功输入时使“返回TRUE”
- 检查一个元素是否有一个类与另一个类总是返回true
- 通过AJAX从具有LazyLoadingEnabled=true的EF返回的数组不正确
- JavaScript函数未返回true
- 如果URL中有任何字符串变体匹配,则返回true
- 使用!而in运算符在应该为true时返回false,为什么
- 如何使以下函数返回true或false
- 为什么 javascript 返回对象位置 true 或 false
- 使用“in”关键字检查 0 始终返回 true
- hasOwnProperty 在对照父对象属性进行检查时返回 true
- 为什么 1.2 == true 返回 false 如果布尔值 (1.2) 实际上是真的
- Backbone.js集合.Create函数没有使用wait: true返回更新后的模型
- Sequelize:调用.get({plain: true}))返回.get不是函数
- 在if语句中为true, true返回false
- 固定脚和外高度(true)返回值