Show red border for all invalid fields after submitting form angularjs Show red border for all invalid fields after submitting form angularjs angularjs angularjs

Show red border for all invalid fields after submitting form angularjs


Reference article: Show red color border for invalid input fields angualrjs

I used ng-class on all input fields.like below

<input type="text" ng-class="{submitted:newEmployee.submitted}" placeholder="First Name" data-ng-model="model.firstName" id="FirstName" name="FirstName" required/>

when I click on save button I am changing newEmployee.submitted value to true(you can check it in my question). So when I click on save, a class named submitted gets added to all input fields(there are some other classes initially added by angularjs).

So now my input field contains classes like this

class="ng-pristine ng-invalid submitted"

now I am using below css code to show red border on all invalid input fields(after submitting the form)

input.submitted.ng-invalid{  border:1px solid #f00;}

Thank you !!

Update:

We can add the ng-class at the form element instead of applying it to all input elements. So if the form is submitted, a new class(submitted) gets added to the form element. Then we can select all the invalid input fields using the below selector

form.submitted .ng-invalid{    border:1px solid #f00;}


I have created a working CodePen example to demonstrate how you might accomplish your goals.

I added ng-click to the <form> and removed the logic from your button:

<form name="addRelation" data-ng-click="save(model)">...<input class="btn" type="submit" value="SAVE" />

Here's the updated template:

<section ng-app="app" ng-controller="MainCtrl">  <form class="well" name="addRelation" data-ng-click="save(model)">    <label>First Name</label>    <input type="text" placeholder="First Name" data-ng-model="model.firstName" id="FirstName" name="FirstName" required/><br/>    <span class="text-error" data-ng-show="addRelation.submitted && addRelation.FirstName.$invalid">First Name is required</span><br/>    <label>Last Name</label>    <input type="text" placeholder="Last Name" data-ng-model="model.lastName" id="LastName" name="LastName" required/><br/>    <span class="text-error" data-ng-show="addRelation.submitted && addRelation.LastName.$invalid">Last Name is required</span><br/>    <label>Email</label>    <input type="email" placeholder="Email" data-ng-model="model.email" id="Email" name="Email" required/><br/>    <span class="text-error" data-ng-show="addRelation.submitted && addRelation.Email.$error.required">Email address is required</span>    <span class="text-error" data-ng-show="addRelation.submitted && addRelation.Email.$error.email">Email address is not valid</span><br/>    <input class="btn" type="submit" value="SAVE" />    </form></section>

and controller code:

app.controller('MainCtrl', function($scope) {    $scope.save = function(model) {    $scope.addRelation.submitted = true;    if($scope.addRelation.$valid) {      // submit to db      console.log(model);     } else {      console.log('Errors in form data');    }  };});

I hope this helps.