我尝试使用ng-blur验证两个字段并且工作正常。但我需要在ng-blur和ng-submit中验证字段。
验证应该同时适用于焦点和点击按钮。
Html:
<form name="myForm" ng-submit="submit()" novalidate>
<input type="text" placeholder="enter your email" name="email"
ng-model="email" ng-blur="visitedEmail = true" required
ng-pattern="pattern"
ng-class="{error: submitted && myForm.email.$invalid }" />
<input type="password" placeholder="enter your password" name="password"
ng-blur="visitedPassword = true" required ng-model="password"
ng-class="{error: submitted && myForm.password.$invalid }" />
<section >
<input type="submit" value="Sign in" />
</section>
</form>
Js文件:
$scope.submit = function(){
// Set the 'submitted' flag to true
$scope.submitted = true;
// Send the form to server
// $http.post
};
任何人都可以告诉我的代码是否错误..
答案 0 :(得分:1)
从表单标记中删除了novalidate。现在验证。如果您按照我在问题评论中提到的那样擦拭,请在提交功能中手动添加验证功能。请参阅plnkr
<form name="myForm" ng-submit="submit()">
<input type="text" placeholder="enter your email" name="email"
ng-model="email" ng-blur="visitedEmail = true" required
ng-pattern="pattern"
ng-class="{error: submitted && myForm.email.$invalid }" />
<input type="password" placeholder="enter your password" name="password"
ng-blur="visitedPassword = true" required ng-model="password"
ng-class="{error: submitted && myForm.password.$invalid }" />
<section >
<input type="submit" value="Sign in" />
</section>
</form>