admin管理员组文章数量:1419223
I have a password field in my registration form. The following is the code for javascript validation.
if(document.getElementById('txtPassword').value == '' || document.getElementById('txtPassword').value == 'Password' ){
alert("Please enter Password");
document.getElementById('txtPassword').focus();
return false;
}
else if (document.getElementById('txtPassword').value.length < 8)
{
alert('Please ensure your password has at least eight characters, a mix of upper and lowercase and at least one number');
return false;
}
else if ( (! document.getElementById('txtPassword').value.match(/[a-z]/) ) || (! document.getElementById('txtPassword').value.match(/[A-Z]/) ) ) {
alert('Please ensure your password has at least eight characters, a mix of upper and lowercase and at least one number');
return false;
}
else if (!document.getElementById('txtPassword').value.match(/\d+/)) {
alert('Please ensure your password has at least eight characters, a mix of upper and lowercase and at least one number');
return false;
}
else if (document.getElementById('txtPassword').length < 8)
{
alert('Please ensure your password has at least eight characters, a mix of upper and lowercase and at least one number');
return false;
}
I need to check for single quotes. If the user enters single quotes in the password field and hit enter the error "Avoid single quotes in this filed" should popup.
How to do that?
I have a password field in my registration form. The following is the code for javascript validation.
if(document.getElementById('txtPassword').value == '' || document.getElementById('txtPassword').value == 'Password' ){
alert("Please enter Password");
document.getElementById('txtPassword').focus();
return false;
}
else if (document.getElementById('txtPassword').value.length < 8)
{
alert('Please ensure your password has at least eight characters, a mix of upper and lowercase and at least one number');
return false;
}
else if ( (! document.getElementById('txtPassword').value.match(/[a-z]/) ) || (! document.getElementById('txtPassword').value.match(/[A-Z]/) ) ) {
alert('Please ensure your password has at least eight characters, a mix of upper and lowercase and at least one number');
return false;
}
else if (!document.getElementById('txtPassword').value.match(/\d+/)) {
alert('Please ensure your password has at least eight characters, a mix of upper and lowercase and at least one number');
return false;
}
else if (document.getElementById('txtPassword').length < 8)
{
alert('Please ensure your password has at least eight characters, a mix of upper and lowercase and at least one number');
return false;
}
I need to check for single quotes. If the user enters single quotes in the password field and hit enter the error "Avoid single quotes in this filed" should popup.
How to do that?
Share Improve this question asked Aug 4, 2012 at 9:28 designersvsoftdesignersvsoft 1,85910 gold badges39 silver badges66 bronze badges 2-
Instead of looking up the element and its value in each
if
statement, just do it once at the beginning and store it in a variable. – Felix Kling Commented Aug 4, 2012 at 9:32 - possible duplicate of How to tell if a string contains a certain character in javascript? – Felix Kling Commented Aug 4, 2012 at 9:33
2 Answers
Reset to default 4else if (document.getElementById('txtPassword').value.indexOf("'") != -1) {
alert("Avoid single quotes in this field");
return false;
}
here is a simple test case. If you make an html page, put this in the head tag, open it in a browser, you'll see it works:
var value1 = "abc'def";
var value2 = "abcdef";
if(value1.indexOf("'") != -1)
alert(value1 + " contains a '");
else
alert(value1 + " does not contain a '");
if(value2.indexOf("'") != -1)
alert(value2 + " contains a '");
else
alert(value2 + " does not contain a '");
Use this regex for password check
^(?=.*\d)(?=.*[a-zA-Z]).{8,}
Currently you are doing lot of validation checks, to avoid those just use above regex.
if (! /^(?=.*\d)(?=.*[a-zA-Z]).{8,}/.test(txtPass)) {
flag = false;
}
else if(txtPass.indexOf("'") != -1) {
flag = false;
}
if (!flag)
alert("Please ensure your password has at least eight characters, a mix of upper and lowercase and at least one number");
Refer LIVE DEMO
本文标签: javascript validation of single quotesStack Overflow
版权声明:本文标题:javascript validation of single quotes - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745296531a2652110.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论