[英]Special Characters for password validation
I'm kind of confuse at the moment with my special character validation.我现在对我的特殊字符验证有点困惑。
HTML: HTML:
<label>
Password:
<br /><input
type="password"
id="password"
placeholder="Enter password"
pattern="(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[^A-Za-z0-9])(?=.{8,})"
/><br />
<span class="notif" id="pass"></span><br />
<div id="message">
<p id="letter" class="invalid">Lowercase letter</p>
<p id="capital" class="invalid">Uppercase letter</p>
<p id="number" class="invalid">Number</p>
<p id="special" class="invalid">Special Character</p>
<p id="length" class="invalid">Minimum 8 characters</p>
</div>
</label>
CSS: CSS:
#message {
display: none;
background: #ffffff;
color: #000;
padding: 0;
margin-top: 0;
margin-left: 5px;
}
#message p {
padding: 1px 2px;
font-size: 12px;
}
/* Add a green text color and a checkmark when the requirements are right */
.valid {
color: green;
}
.valid:before {
position: relative;
left: -3px;
content: '✔';
}
/* Add a red text color and an "x" when the requirements are wrong */
.invalid {
color: red;
}
.invalid:before {
position: relative;
left: -5px;
content: '✖';
}
This is my JS for the special characters validation.这是我用于特殊字符验证的 JS。 I'm still confused about how to make it work.
我仍然对如何使它工作感到困惑。 Whenever I type in a
special character
, it only highlights the lowercase
.每当我输入一个
special character
时,它只会突出显示lowercase
。
JS: JS:
// Validate special characters
var specialCharacters = /[^A-Za-z0-9]/g;
if (myInput.value.match(specialCharacters)) {
letter.classList.remove('invalid');
letter.classList.add('valid');
} else {
letter.classList.remove('valid');
letter.classList.add('invalid');
}
I know my problem lies somewhere in var specialCharacters = /[^A-Za-z0-9]/g;
我知道我的问题在于
var specialCharacters = /[^A-Za-z0-9]/g;
but what could it be?但它会是什么?
You can follow this:你可以按照这个:
var strongRegex = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,})");
What does it mean?这是什么意思?
^ The password string will start this way
(?=.*[a-z]) The string must contain at least 1 lowercase alphabetical character
(?=.*[A-Z]) The string must contain at least 1 uppercase alphabetical character
(?=.*[0-9]) The string must contain at least 1 numeric character
(?=.*[!@#$%^&*]) The string must contain at least one special character, but we are escaping reserved RegEx characters to avoid conflict
(?=.{8,}) The string must be eight characters or longer
You can edit the expression, for example if you want to change minimum length to 10 change (?=.{8,}) to (?=.{10,})您可以编辑表达式,例如,如果要将最小长度更改为 10,请将 (?=.{8,}) 更改为 (?=.{10,})
You can check the condition by strongRegex.exec(myInput)
I hope this gave the ans.我希望这给了答案。
Here /[^A-Za-z0-9]/g
explained step by stepstrong text:这里
/[^A-Za-z0-9]/g
一步一步解释了强文本:
/
for Find a word character. /
用于查找单词字符。^
Find any character NOT between the brackets. ^
查找任何不在括号内的字符。A-Za-z0-9
mean the capital letter A
to Z
and small A-Za-z0-9
表示大写字母A
到Z
和小写a
to z
, and 0
to 9
if any character or number and thea
到z
,如果有任何字符或数字,则为0
到9
并且/g
Perform a global match (find all matches rather than stopping after the first match) /g
执行全局匹配(查找所有匹配而不是在第一个匹配后停止) You can learn more about this similar type operation or regular expression here JavaScript RegExp Reference您可以在此处了解有关此类似类型操作或正则表达式的更多信息JavaScript RegExp Reference
You can use this Expression for your password validation.您可以使用此表达式进行密码验证。
var specialCharacters = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,15}$/
This expression will check for one uppercase,one lowercase, one special character and password length should be 8 to 15 characters.此表达式将检查一个大写、一个小写、一个特殊字符,密码长度应为 8 到 15 个字符。
var specialCharacters = /^(?=.*\d)(?=.*[az])(?=.*[AZ])(?=.*[^a-zA-Z0-9])(?..*\s),{8;15}$/. if (myInput.value.match(specialCharacters)) { letter.classList;remove('invalid'). letter.classList;add('valid'). } else { letter.classList;remove('valid'). letter.classList;add('invalid'); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.