简体   繁体   English

使用AngularJS在提交时将空输入字段设置为ng-invalid?

[英]Set empty input field to ng-invalid on submit using AngularJS?

I want to set input field to ng-invalid if a user clicks submit and the form fields are empty using AngularJS. 如果用户单击“提交”并且使用AngularJS表单字段为空,我想将输入字段设置为ng-invalid。 I know a directive can be used to custom form validation and using $setValidity to manually set the input field but I'm not sure I need a directive to just highlight the empty/required fields on submit. 我知道一个指令可以用于自定义表单验证,并使用$ setValidity手动设置输入字段,但我不确定我需要一个指令来突出显示提交时的空/必填字段。 Any help would be greatly appreciated. 任何帮助将不胜感激。 Thank you. 谢谢。

Here is the code I am working with: HTML 这是我正在使用的代码:HTML

<form name="SignUpForm ng-submit="submitUser()" novalidate>
<input type="text" name="dob" ng-model="dob" placeholder="Date of Birth" required />
<span class="error" ng-show="signUpForm.dob.$dirty && signUpForm.dob.$invalid">Please provide a valid date of birth</span>
<input type="text" name="email" ng-model="email" ng-pattern="/^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/" placeholder="Email" required />
<span class="error" ng-show="signUpForm.email.$dirty && signUpForm.email.$invalid">Please provide a valid email address</span>
<button type="submit">Submit</submit>
</form>

SASS/CSS SASS / CSS

input {
&.ng-valid.ng-dirty {
color: #fff;
background-color: rgba(146, 253, 156, 0.5);
border-color: #92fd9c;
box-shadow: inset 2px 2px 2px #92fd9c;
}
&.ng-invalid.ng-dirty {
color: #850000;
background: rgba(254, 186, 186, 0.5);
border: 1px solid #850000;
}

AngularJS AngularJS

$scope.submitUser = function() {
if ($scope.signUpForm.$valid) {
// Set form values & construct data to send
} else {
$scope.response = "Please fill in the required fields";
}

I'm not sure I completely follow, but does this do what you want? 我不确定我是否完全遵循,但这样做你想要的吗?

Plunker: Plunker:

http://plnkr.co/edit/L5qzEJRKkdGogsE7IzAr?p=preview http://plnkr.co/edit/L5qzEJRKkdGogsE7IzAr?p=preview

HTML: HTML:

<form name="signUpForm" ng-submit="submitUser()" novalidate>
<input type="text" ng-class="{ errorinput: submitted && signUpForm.dob.$invalid }" name="dob" ng-model="dob" placeholder="Date of Birth" required />
<span class="e" ng-show="submitted && signUpForm.dob.$invalid">Please provide a valid date of birth</span>
<br>
<input type="text" name="email" ng-class="{ errorinput: submitted && signUpForm.email.$invalid }" ng-model="email" ng-pattern="/^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$/" placeholder="Email" required />
<span class="e" ng-show="submitted && signUpForm.email.$invalid">Please provide a valid email address</span>
<br>
<button type="submit">Submit</button>
</form>

Javascript: 使用Javascript:

$scope.submitUser = function() {
  if ($scope.signUpForm.$valid) {

  } else {
    $scope.submitted = true;
  }
}

Add a ng-class into the specific input like textbox and set an invalid class on HTML. 将ng-class添加到特定输入(如文本框)中,并在HTML上设置无效的类。 I am using kendo-UI so, I set k-invalid: true when ever it fails the condition. 我正在使用kendo-UI,所以我设置了k-invalid:当它失败的时候是真的。

<input ng-class="{'k-invalid': (condition === false)}" required>

this will highlight the input value as soon as the condition is false. 一旦条件为假,这将突出显示输入值。

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

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