Javascript 检查密码和确认密码是否相同的验证无效
这是我的密码:Javascript 检查密码和确认密码是否相同的验证无效,javascript,Javascript,这是我的密码: function validate_form(thisform) { with (thisform) { if (validate_required(name,"Name must be filled out!")==false) {name.focus();return false;} if (validate_required(country," Country must be filled out!")==false) {country.focu
function validate_form(thisform)
{
with (thisform)
{
if (validate_required(name,"Name must be filled out!")==false)
{name.focus();return false;}
if (validate_required(country," Country must be filled out!")==false)
{country.focus();return false;}
if (validate_required(state,"State must be filled out!")==false)
{state.focus();return false;}
if (validate_required(city,"City must be filled out!")==false)
{city.focus();return false;}
if (validate_required(contact,"Contact must be filled out!")==false)
{contact.focus();return false;}
if (validate_required(emailid,"Email must be filled out!")==false)
{emailid.focus();return false;}
if (validate_email(userid,"Email is not valid")==false)
{userid.focus();return false;}
if (validate_required(password,"pasword must be filed out")==false)
{password.focus();return false;}
if (validate_required(cpassword,"Password must be confirmed")==false)
{cpassword.focus();return false;}
if(validate_required((password.value != cpassword.value),"Your password and confirmation password do not match.")==false) {
cpassword.focus();return false;
}
所有其他验证都在工作,但不是最后一个。为什么会这样以及如何修复它?验证所需的函数似乎希望使用HTML表单控件,例如,文本输入字段作为第一个参数,并检查是否存在值。在这种情况下,这不是你想要的 此外,当您写入['password'].value时,您将创建一个长度为1的新数组,其中包含字符串“password”,然后从中读取不存在的属性值,从而生成未定义的值 您可能想尝试的是:
if (password.value != cpassword.value) { cpassword.focus(); return false; }
您还需要以某种方式编写错误消息,但我无法从代码中看出这是如何完成的。validate_required函数似乎需要一个HTML表单控件,例如,文本输入字段作为第一个参数,并检查是否有值。在这种情况下,这不是你想要的 此外,当您写入['password'].value时,您将创建一个长度为1的新数组,其中包含字符串“password”,然后从中读取不存在的属性值,从而生成未定义的值 您可能想尝试的是:
if (password.value != cpassword.value) { cpassword.focus(); return false; }
您还需要以某种方式编写错误消息,但我无法从您的代码中看出这是如何完成的。我想您已经从该页面获得了验证所需的函数: 在这种情况下,您的最后一个条件将不会像您预期的那样工作 您可以将其替换为以下内容:
if (password.value != cpassword.value) {
alert("Your password and confirmation password do not match.");
cpassword.focus();
return false;
}
我想您已经从这个页面获得了validate_所需的函数: 在这种情况下,您的最后一个条件将不会像您预期的那样工作 您可以将其替换为以下内容:
if (password.value != cpassword.value) {
alert("Your password and confirmation password do not match.");
cpassword.focus();
return false;
}
}
}第1步:
创建ts:app/_helpers/must-match.validator.ts
import { FormGroup } from '@angular/forms';
export function MustMatch(controlName: string, matchingControlName: string) {
return (formGroup: FormGroup) => {
const control = formGroup.controls[controlName];
const matchingControl = formGroup.controls[matchingControlName];
if (matchingControl.errors && !matchingControl.errors.mustMatch) {
return;
}
if (control.value !== matchingControl.value) {
matchingControl.setErrors({ mustMatch: true });
} else {
matchingControl.setErrors(null);
}
}
}
步骤2:
在component.ts中使用
import { MustMatch } from '../_helpers/must-match.validator';
ngOnInit() {
this.loginForm = this.formbuilder.group({
Password: ['', [Validators.required, Validators.minLength(6)]],
ConfirmPassword: ['', [Validators.required]],
}, {
validator: MustMatch('Password', 'ConfirmPassword')
});
}
步骤3:
在视图/Html中使用
<input type="password" formControlName="Password" class="form-control" autofocus>
<div *ngIf="loginForm.controls['Password'].invalid && (loginForm.controls['Password'].dirty || loginForm.controls['Password'].touched)" class="alert alert-danger">
<div *ngIf="loginForm.controls['Password'].errors.required">Password Required. </div>
<div *ngIf="loginForm.controls['Password'].errors.minlength">Password must be at least 6 characters</div>
</div>
<input type="password" formControlName="ConfirmPassword" class="form-control" >
<div *ngIf="loginForm.controls['ConfirmPassword'].invalid && (loginForm.controls['ConfirmPassword'].dirty || loginForm.controls['ConfirmPassword'].touched)" class="alert alert-danger">
<div *ngIf="loginForm.controls['ConfirmPassword'].errors.required">ConfirmPassword Required. </div>
<div *ngIf="loginForm.controls['ConfirmPassword'].errors.mustMatch">Your password and confirmation password do not match.</div>
</div>
步骤1:
创建ts:app/_helpers/must-match.validator.ts
import { FormGroup } from '@angular/forms';
export function MustMatch(controlName: string, matchingControlName: string) {
return (formGroup: FormGroup) => {
const control = formGroup.controls[controlName];
const matchingControl = formGroup.controls[matchingControlName];
if (matchingControl.errors && !matchingControl.errors.mustMatch) {
return;
}
if (control.value !== matchingControl.value) {
matchingControl.setErrors({ mustMatch: true });
} else {
matchingControl.setErrors(null);
}
}
}
步骤2:
在component.ts中使用
import { MustMatch } from '../_helpers/must-match.validator';
ngOnInit() {
this.loginForm = this.formbuilder.group({
Password: ['', [Validators.required, Validators.minLength(6)]],
ConfirmPassword: ['', [Validators.required]],
}, {
validator: MustMatch('Password', 'ConfirmPassword')
});
}
步骤3:
在视图/Html中使用
<input type="password" formControlName="Password" class="form-control" autofocus>
<div *ngIf="loginForm.controls['Password'].invalid && (loginForm.controls['Password'].dirty || loginForm.controls['Password'].touched)" class="alert alert-danger">
<div *ngIf="loginForm.controls['Password'].errors.required">Password Required. </div>
<div *ngIf="loginForm.controls['Password'].errors.minlength">Password must be at least 6 characters</div>
</div>
<input type="password" formControlName="ConfirmPassword" class="form-control" >
<div *ngIf="loginForm.controls['ConfirmPassword'].invalid && (loginForm.controls['ConfirmPassword'].dirty || loginForm.controls['ConfirmPassword'].touched)" class="alert alert-danger">
<div *ngIf="loginForm.controls['ConfirmPassword'].errors.required">ConfirmPassword Required. </div>
<div *ngIf="loginForm.controls['ConfirmPassword'].errors.mustMatch">Your password and confirmation password do not match.</div>
</div>
@bhavna raghuvanshi:变量password和cpassword是否设置为包含DOM元素?例如,函数中的alertpassword.value显示了什么?您不能使用编写的validate_required函数,因为它需要一个输入元素并检查它是否为空。也就是说,你根本不应该为最后一个案子打电话。您应该查看validate_required的定义,看看它是如何报告错误消息的,然后在两个密码字段的值不相同时在此处执行相同的操作。我已经检查了所有内容。所有其他验证都在工作,并显示错误消息作为参数传递。@bhavna raghuvanshi:变量password和cpassword是否已设置让它们包含DOM元素?例如,函数中的alertpassword.value显示了什么?您不能使用编写的validate_required函数,因为它需要一个输入元素并检查它是否为空。也就是说,你根本不应该为最后一个案子打电话。您应该查看validate_required的定义,了解它如何报告错误消息,然后在两个密码字段的值不相同时在此处执行相同的操作。我已检查了所有内容。所有其他验证都在工作,并显示作为参数传递的错误消息。在if password.value之前!=cpassword.value{添加这个:alertpassword.value!=cpassword.value;你得到了什么?我没有得到任何东西添加这个你根本没有得到一个警报弹出窗口???试试看alertpassword.value;alertcpassword.value;你从这两个字段中都得到了值吗?你能发布更多的原始/更改的代码吗?很难猜出什么是潜在的打字错误,什么是错误如果password.value!=cpassword.value,在前面可能是一个真正的问题{添加这个:alertpassword.value!=cpassword.value;你得到了什么?我没有得到任何东西添加这个你根本没有得到一个警报弹出窗口???试试看alertpassword.value;alertcpassword.value;你从这两个字段中都得到了值吗?你能发布更多的原始/更改的代码吗?很难猜出什么是潜在的打字错误,什么是错误这将是一个真正的问题。嗨,请抢夺并改进你的答案。嗨,请抢夺并改进你的答案。