简体   繁体   English

如何使用 JavaScript Angular 比较新密码和确认密码

[英]How to compare new password and confirm password using JavaScript Angular

I need to compare below new password field and confirm password field.我需要比较下面的新密码字段并确认密码字段。 This is the form这是表格

    <form novalidate (ngSubmit)="changepasswordform.form.valid && changePassword(this.changepassword, changepasswordform)"
    #changepasswordform="ngForm">
 <div class="form-group row">
                    <label for="email5" class="col-sm-5 col-md-5 col-form-label">New Password</label>
                    <div class="col-sm-7 col-md-7">
                        <input type="password" class="form-control" pattern="^(?=.*[A-Za-z])(?=.*\d).{8,}$" required
                            [class.is-invalid]="newpassword.invalid && newpassword.touched" name="newpasswords"
                            #newpassword="ngModel" [(ngModel)]="changepassword.newPassword"
                            placeholder="New Password">

                        <div [class.d-none]="newpassword.valid || newpassword.untouched">
                            <small class=" text-danger" *ngIf="newpassword.errors?.required">Password is
                                required</small>
                            <small *ngIf="newpassword.hasError('pattern')" class=" text-danger">
                                Password should be 8 characters, at least one
                                letter and one number</small>
                        </div>
                    </div>
                </div>

<div class="form-group row">
                    <label for="email5" class="col-sm-5 col-md-5 col-form-label">Confirm Password</label>
                    <div class="col-sm-7 col-md-7">
                        <input type="password" class="form-control"
                            [class.is-invalid]="repassword.invalid && repassword.touched" name="repassword"
                            #repassword="ngModel" placeholder="Confirm Password" compare="newpassword"
                            [(ngModel)]="changepassword.repassword" required>

                        <div [class.d-none]="repassword.valid || repassword.untouched">
                            <small *ngIf="repassword.errors?.required" class="text-danger">Confirm password is
                                required</small>
                        </div>

                        <div class="text-danger"
                            *ngIf="repassword.invalid && (repassword.dirty || repassword.touched)">
                            <small class="login-val" *ngIf="repassword.errors['compare']"> Confirm password do not
                                match</small>
                        </div>

                    </div>
                </div>
</form>

When user type the text box, validation should be check.当用户输入文本框时,应检查验证。 Do you know how to do this.你知道怎么做吗。 I need to do this without any library.我需要在没有任何库的情况下执行此操作。

Since you are not using reactive forms, you can directly perform a comparison between the models newPassword and repassword to set the error message.由于您没有使用反应式表单,您可以直接在模型 newPassword 和 repassword 之间进行比较以设置错误消息。 Please refer following stackblitz.请参考以下stackblitz。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM