我正在尝试简单地检查密码是否具有大写,小写,特殊字符和数字,但是无论我使用转换大小写还是一系列的if if条件,它都会丢弃到“不能识别字符”。
为什么?
以下是我的声明:
public str: string;
public lowerCaseLetter: boolean;
public upperCaseLetter: boolean;
public specialChar: boolean;
public numChar: boolean;
public specialCharPattern = new RegExp(
/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/g
);
public lowerCharPattern = new RegExp(/a-z/g);
public upperCharPattern = new RegExp(/A-Z/g);
public numCharPattern = new RegExp(/0-9/g);
我也尝试过:
public specialCharPattern = new RegExp(
/[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/
);
public lowerCharPattern = new RegExp("/a-z/");
public upperCharPattern = new RegExp("/A-Z/");
public numCharPattern = new RegExp("/0-9/");
我也尝试过:
public specialCharPattern = /[~`!#$%\^&*+=\-\[\]\\';,/{}|\\":<>\?]/;
public lowerCharPattern = /a-z/;
public upperCharPattern = /A-Z/;
public numCharPattern = /0-9/;
以下是onKeyUp处理程序:
onKeyUp($event) {
this.str = $event.target.value;
console.log(this.str);
if (this.lowerCharPattern.test(this.str)) {
this.lowerCaseLetter = true;
console.log("lower case");
} else if (this.upperCharPattern.test(this.str)) {
this.upperCaseLetter = true;
console.log("upper case");
} else if (this.specialCharPattern.test(this.str)) {
this.specialChar = true;
console.log("special char");
} else if (this.numCharPattern.test(this.str)) {
this.numChar = true;
console.log("number");
} else {
console.log("can't recognize the char");
}
this.str = "";
}
以下是html的一部分:
<label for="password"
>Please enter a password
<input
type="password"
class="form-control"
[(ngModel)]="model.password"
#password="ngModel"
[ngClass]="{
'is-invalid': f.submitted && password.invalid
}"
name="password"
placeholder="Password"
(keyup)="onKeyUp($event)"
required
minlength="6"
/>min. length of 6 characters
</label>
我可以在控制台中看到,我键入的内容都是通过onKeyUp传递的内容,但是在任何情况下都不会停止。另外,我希望输入的每个键都将清除“ this.str”-this.str =“”;但它仍在继续。这是因为$ event.target.value传递了整个输入字段吗?
和往常一样,谢谢您
答案 0 :(得分:0)
由于@ wiktor-stribi%c5%bcew和@corion都发表了评论,因此我能够使用以下代码进行操作:
this.str = $event.target.value;
this.pswArr = this.str.split("");
for (let x = 0; x <= this.pswArr.length; x++) {
if (this.lowerCharPattern.test(this.pswArr[x])) {
this.lowerCaseLetter = true;
this.lowerCharArr[0] = 1;
this.strngMeter += 1;
// console.log("lower case");
}
if (this.upperCharPattern.test(this.pswArr[x])) {
this.upperCaseLetter = true;
this.upperCharArr[0] = 1;
this.strngMeter += 1;
// console.log("upper case");
}
if (this.specialCharPattern.test(this.pswArr[x])) {
this.specialChar = true;
this.specialCharArr[0] = 1;
this.strngMeter += 1;
// console.log("special char");
}
if (this.numCharPattern.test(this.pswArr[x])) {
this.numChar = true;
this.numCharArr[0] = 1;
this.strngMeter += 1;
// console.log("number");
}
// console.log("this.pswArr: ", this.pswArr);
}