Angularjs 密码和确认密码的验证无效
我是新来安格拉斯的。我正在尝试进行表单验证,包括usename、email、password和confirm password,但当我正确填写所有字段时,submit按钮仍处于禁用状态。有人能帮我吗Angularjs 密码和确认密码的验证无效,angularjs,Angularjs,我是新来安格拉斯的。我正在尝试进行表单验证,包括usename、email、password和confirm password,但当我正确填写所有字段时,submit按钮仍处于禁用状态。有人能帮我吗 <div ng-app="myapp"> <form name="myForm"> <label for="email">Email</label> <input type="email" id="email" name="em
<div ng-app="myapp">
<form name="myForm">
<label for="email">Email</label>
<input type="email" id="email" name="email" ng-model="formData.email" required/>
<span ng-show="myForm.email.$error.required && myForm.email.$dirty">required</span>
<span ng-show="!myForm.email.$error.required && myForm.email.$error.email && myForm.email.$dirty">invalid email</span>
<br />
<label for="email">Name</label>
<input type="text" id="name" name="name" ng-model="formData.name" required/>
<span ng-show="myForm.name.$error.required && myForm.name.$dirty">required</span>
<br />
<label for="username">Username</label>
<input type="text" id="username" name="username" ng-model="formData.username" ng-minlength="5" ng-maxlength="20" ng-pattern="/^[A-z][A-z0-9]*$/" required />
<span ng-show="myForm.username.$error.required && myForm.username.$dirty">required</span>
<span ng-show="!myForm.username.$error.minLength && !myForm.username.$error.maxLength && myForm.username.$error.pattern && myForm.username.$dirty">Must start with a letter, and contain letters & numbers only.</span>
<span ng-show="!myForm.username.$error.required && (myForm.username.$error.minlength || myForm.username.$error.maxlength) && myForm.username.$dirty">Username ust be between 5 and 20 characters.</span>
<br />
<label for="password">Password</label>
<input type="password" id="password" name="password" ng-model="formData.password" ng-minlength="8" ng-maxlength="20" required />
<span ng-show="myForm.password.$error.required && myForm.password.$dirty">required</span>
<span ng-show="!myForm.password.$error.required && (myForm.password.$error.minlength || myForm.password.$error.maxlength) && myForm.password.$dirty">Passwords must be between 8 and 20 characters.</span>
<br />
<label for="password_c">Confirm Password</label>
<input type="password" id="password_c" name="password_c" ng-model="formData.password_c" valid-password-c required />
<span ng-show="myForm.password_c.$error.required && myForm.password_c.$dirty">Please confirm your password.</span>
<span ng-show="!myForm.password_c.$error.required && myForm.password_c.$error.noMatch && myForm.password.$dirty">Passwords do not match.</span>
<br />
<button type="submit" class="btn" ng-disabled="!myForm.$valid">Submit</button>
</form>
</div>
<script>
var app = angular.module('myapp', ['UserValidation']);
angular.module('UserValidation', []).directive('validPasswordC', function () {
return {
require: 'ngModel',
link: function (scope, elm, attrs, ctrl) {
ctrl.$parsers.unshift(function (viewValue, $scope) {
var noMatch = viewValue != scope.myForm.password.$viewValue
ctrl.$setValidity('noMatch', !noMatch)
})
}
}
})
</script>
电子邮件
必修的
无效电子邮件
名称
必修的
用户名
必修的
必须以字母开头,并包含字母&;只有数字。
用户名必须介于5到20个字符之间。
密码
必修的
密码必须介于8到20个字符之间。
确认密码
请确认您的密码。
密码不匹配。
提交
var app=angular.module('myapp',['UserValidation']);
angular.module('UserValidation',[])指令('validPasswordC',函数(){
返回{
要求:'ngModel',
链接:函数(范围、elm、属性、ctrl){
ctrl.$parsers.unshift(函数(viewValue,$scope){
var noMatch=viewValue!=scope.myForm.password.$viewValue
ctrl.$setValidity('noMatch',!noMatch)
})
}
}
})
希望这有帮助。您应该返回noMatch
<script>
var app = angular.module('myapp', ['UserValidation']);
angular.module('UserValidation', []).directive('validPasswordC', function () {
return {
require: 'ngModel',
link: function (scope, elm, attrs, ctrl) {
ctrl.$parsers.unshift(function (viewValue, $scope) {
var noMatch = viewValue != scope.myForm.password.$viewValue
ctrl.$setValidity('noMatch', !noMatch);
return (noMatch)?noMatch:!noMatch;
})
}
}
})
</script>
var app=angular.module('myapp',['UserValidation']);
angular.module('UserValidation',[])指令('validPasswordC',函数(){
返回{
要求:'ngModel',
链接:函数(范围、elm、属性、ctrl){
ctrl.$parsers.unshift(函数(viewValue,$scope){
var noMatch=viewValue!=scope.myForm.password.$viewValue
ctrl.$setValidity('noMatch',!noMatch);
返回(noMatch)?noMatch:!noMatch;
})
}
}
})
希望这有帮助。您应该返回noMatch
<script>
var app = angular.module('myapp', ['UserValidation']);
angular.module('UserValidation', []).directive('validPasswordC', function () {
return {
require: 'ngModel',
link: function (scope, elm, attrs, ctrl) {
ctrl.$parsers.unshift(function (viewValue, $scope) {
var noMatch = viewValue != scope.myForm.password.$viewValue
ctrl.$setValidity('noMatch', !noMatch);
return (noMatch)?noMatch:!noMatch;
})
}
}
})
</script>
var app=angular.module('myapp',['UserValidation']);
angular.module('UserValidation',[])指令('validPasswordC',函数(){
返回{
要求:'ngModel',
链接:函数(范围、elm、属性、ctrl){
ctrl.$parsers.unshift(函数(viewValue,$scope){
var noMatch=viewValue!=scope.myForm.password.$viewValue
ctrl.$setValidity('noMatch',!noMatch);
返回(noMatch)?noMatch:!noMatch;
})
}
}
})