javascript验证代码不起作用
javascript validation code wont work
我正在编写一个验证脚本,但当我运行代码时,除了所有文本字段边框变红之外,什么都没有发生(这应该发生,但在文本字段中有文本的条件下,它仍然是红色的)。这是我的代码
function frontendvalidation(){
var emailaddbox = document.getElementById('emailaddbox').value;
//var emailaddbox = document.forms["signupform"]["emailaddbox"].value;
var username = document.getElementById('username').value;
var password = document.getElementById('passwordbox').value;
var confirmpasswordbox = document.getElementById('confirmpasswordbox').value;
// if our signup fields are empty then the border will be red
if(username === '' || emailaddbox === '' || password=== '' || confirmpasswordbox=== ''){
document.getElementById('username'). style.borderColor = "red";
document.getElementById('emailaddbox').style.borderColor = "red";
document.getElementById('passwordbox').style.borderColor = "red";
document.getElementById('confirmpasswordbox').style.borderColor = "red";
return false;
}
if(username === !'' && username.length => 5) {
document.getElementById('username'). style.borderColor = "green";
return true;
}
if(emailaddbox === !'' ){
document.getElementById('emailaddbox').style.borderColor = "green";
}
if (password === ! '' && password.length <=5){
document.getElementById('passwordbox').style.borderColor = "green";
}
if ( password != confirmpasswordbox){
document.getElementById('confirmpasswordbox').style.borderColor = "red";
} else if (confirmpasswordbox === password){
document.getElementById('confirmpasswordbox').style.borderColor = "green";
return true ;
}
}
你想用做什么
if (password === ! '' && password.length <=5){
document.getElementById('passwordbox').style.borderColor = "green";
}
试试这样的东西:
if (password != undefined && password.length > 5){
document.getElementById('passwordbox').style.borderColor = "green";
}
基本上password === ! ''
不是有效的语法。您说的类似于"密码等于空字符串的倒数"。对于一些JavaScript类型的体操来说,这相当于"密码严格等于真"。
(此外,强制密码超过5个字符,不少于或等于5个字符似乎更有意义)
除了其他答案中列出的语法错误外,在任何字段中检查空格的行将所有字段标记为错误(如果其中任何字段为空)。这里还有一个语法错误:用户名.长度=>5应该是用户名.长度>=5
这是更新的工作代码:
function frontendvalidation(){
var emailaddbox = document.getElementById('emailaddbox').value;
//var emailaddbox = document.forms["signupform"]["emailaddbox"].value;
var username = document.getElementById('username').value;
var password = document.getElementById('passwordbox').value;
var confirmpasswordbox = document.getElementById('confirmpasswordbox').value;
//Default to everything showing an error, and clear the errors as fields are validated.
document.getElementById('username').style.borderColor = "red";
document.getElementById('emailaddbox').style.borderColor = "red";
document.getElementById('passwordbox').style.borderColor = "red";
document.getElementById('confirmpasswordbox').style.borderColor = "red";
// if our signup fields are empty then the border will be red
if(username === '' && emailaddbox === '' && password=== '' && confirmpasswordbox=== ''){
return false;
}
if(username !== '' && username.length >= 5) {
document.getElementById('username').style.borderColor = "green";
}
if (emailaddbox !== '' ){
document.getElementById('emailaddbox').style.borderColor = "green";
}
if (password !== '' && password.length <=5){
document.getElementById('passwordbox').style.borderColor = "green";
}
if ( password != confirmpasswordbox){
document.getElementById('confirmpasswordbox').style.borderColor = "red";
} else if (confirmpasswordbox === password){
document.getElementById('confirmpasswordbox').style.borderColor = "green";
return true ;
}
}
工作代码笔:http://codepen.io/nobrien/pen/oxqoeR
将条件username === !''
更改为username !== ''
语法!''
every是false
,如果你与一个字符串进行比较,你每次都会获得false
,因为你正在与===
进行比较,它们需要与另一个类似的字符串进行比较才能获得true
的结果,在你的情况下,'' === false
则是每次false
。
我修复了你的代码,发现有很多像=>
这样的语法错误,正确的是>=
。查看我修复的代码。
我想这就是你想要的。
function frontendvalidation(event){
var elementEmail = document.getElementById('emailaddbox');
var elementUserName = document.getElementById('username');
var elementPassword = document.getElementById('passwordbox');
var elementConfirmPassword = document.getElementById('confirmpasswordbox');
var validate = false;
if (elementUserName.value !== '' && elementUserName.value.length >= 5) {
elementUserName.style.borderColor = "green";
validate = validate ? validate : true;
} else {
elementUserName.style.borderColor = "red";
}
if (elementEmail.value !== '') {
elementEmail.style.borderColor = "green";
validate = validate ? validate : true;
} else {
elementEmail.style.borderColor = "red";
}
if (elementPassword.value !== '' && elementPassword.value.length <= 5){
elementPassword.style.borderColor = "green";
validate = validate ? validate : true;
} else {
elementPassword.style.borderColor = "red";
}
if ( elementConfirmPassword.value !== '' && elementConfirmPassword.value === elementPassword.value){
elementConfirmPassword.style.borderColor = "green";
validate = validate ? validate : true;
} else {
elementConfirmPassword.style.borderColor = "red";
}
return validate;
}
document.getElementById('btnValidator').onclick = frontendvalidation;
<input type="text" id="username" /><br/>
<input type="text" id="emailaddbox" /><br/>
<input type="text" id="passwordbox" /><br/>
<input type="text" id="confirmpasswordbox" /><br/>
<button id="btnValidator" >Validator</button>
相关文章:
- 面向对象的Javascript代码在IE7中不起作用
- 为什么indexOf在这个js代码中不起作用
- d3js文本传输-示例代码不起作用
- 滚动动画代码不起作用
- 为什么此验证代码不起作用
- KeyPress和Keydown在这个特定的代码上不起作用.只有key-up起作用
- 我的jquery代码不起作用.为什么?
- 调试此代码?警报不起作用
- I'我不知道为什么我的代码没有'不起作用
- jquery中的PHP代码不起作用
- 谷歌地图代码不起作用
- PHP代码中实现的JavaScript |if语句不起作用
- 为什么这个代码不起作用?我花了很长时间试图弄清楚这一点
- Javascript ajax代码在ibm worklight中不起作用
- 这段jquery代码在angular指令中不起作用
- JavaScript在我下面的代码中不起作用
- JS代码使IE9和Safari冻结,在Opera中不起作用
- 复选框检查器在表中不起作用.代码在没有表标记的情况下工作
- JavaScript 按钮不起作用;代码与其他工作按钮相同
- 视频.js在 Safari 中不起作用(代码:4 MEDIA_ERR_SRC_NOT_SUPPORTED)