这是我的代码:
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;
}
所有其他验证都有效,但不是最后一次验证。为什么会如此以及如何解决?
答案 0 :(得分:6)
我认为你从这个页面获得了validate_required()函数:http://www.w3schools.com/js/js_form_validation.asp?
function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
{
alert(alerttxt);return false;
}
else
{
return true;
}
}
}
在这种情况下,您的上一个条件将无法正常工作。
您可以将其替换为:
if (password.value != cpassword.value) {
alert("Your password and confirmation password do not match.");
cpassword.focus();
return false;
}
答案 1 :(得分:3)
validate_required函数似乎期望将HTML表单控件(例如,文本输入字段)作为第一个参数,并检查那里是否存在值。在这种情况下,这不是你想要的。
此外,当您编写['password'].value
时,您创建一个长度为1的新数组,其中包含string
'password'
,然后从中读取不存在的属性"value"
它,产生未定义的值。
您可能想尝试的是:
if (password.value != cpassword.value) { cpassword.focus(); return false; }
(您还需要以某种方式编写错误消息,但我无法从您的代码中看到它是如何完成的。)。
答案 2 :(得分:1)
function validate()
{
var a=documents.forms["yourformname"]["yourpasswordfieldname"].value;
var b=documents.forms["yourformname"]["yourconfirmpasswordfieldname"].value;
if(!(a==b))
{
alert("both passwords are not matching");
return false;
}
return true;
}
答案 3 :(得分:0)
if((pswd.length<6 || pswd.length>12) || pswd == ""){
document.getElementById("passwordloc").innerHTML="character should be between 6-12 characters"; status=false;
}
else {
if(pswd != pswdcnf) {
document.getElementById("passwordconfirm").innerHTML="password doesnt matched"; status=true;
} else {
document.getElementById("passwordconfirm").innerHTML="password matche";
document.getElementById("passwordloc").innerHTML = '';
}
}
答案 4 :(得分:0)
第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>