Angular toggle class on input if validation is valid - angularjs

I am trying to toggle css class on input if validation is valid, i can do if is invalid, but i need two classes if is valid and if is not. Is it easy to make it if is not valid, but how to add if is valid, here is my code i have for now:
<form name="userForm" ng-submit="submitForm()" novalidate>
<input type="email" name="email" placeholder="E-mailadres" class="input-field" ng-model="user.email" required ng-class="{ 'has-error' : userForm.email.$invalid && !userForm.email.$pristine }" />
<p ng-show="userForm.email.$invalid && !userForm.email.$pristine" class="help-block">Enter a valid email.</p>
<input type="password" name="password" ng-model="user.password" placeholder="Wachtwoord" class="input-field" required />
<p ng-show="userForm.password.$invalid && !userForm.password.$pristine" class="help-block">You name is required.</p>
<button type="submit" class="btn hit-btn" ng-disabled="userForm.$invalid">Log in</button>
</form>
You will notice that i have class has-error but i need also class no-error

Try this...
.has-error {
background: red;
}
.no-error {
background: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app>
<form name="userForm" ng-submit="submitForm()" novalidate>
<input type="email" name="email" placeholder="E-mailadres" class="input-field" ng-model="user.email" required ng-class="{ 'has-error' : userForm.email.$invalid && !userForm.email.$pristine,'no-error' :!userForm.email.$invalid }" />
<p ng-show="userForm.email.$invalid && !userForm.email.$pristine" class="help-block">Enter a valid email.</p>
<input type="password" name="password" ng-model="user.password" placeholder="Wachtwoord" class="input-field" required />
<p ng-show="userForm.password.$invalid && !userForm.password.$pristine" class="help-block">You name is required.</p>
<button type="submit" class="btn hit-btn" ng-disabled="userForm.$invalid">Log in</button>
</form>
</div>

If you need another class, just add it with the opposite condition:
<input type="email" name="email" placeholder="E-mailadres"
class="input-field" ng-model="user.email" required
ng-class="{
'has-error' : userForm.email.$invalid && !userForm.email.$pristine,
'no-error' : !userForm.email.$invalid || userForm.email.$pristine
}"
/>

ng-class="{userForm.email.$invalid && !userForm.email.$pristine?'has-error':'no-error'}"

Related

Facing issues in angularjs validations

When I am clicking login button, error messages are not showing.
<form name="loginform" ng-submit="loginform.$valid && login()" novalidate >
<div class="form-group" ng-class="{ 'has-error' : loginform.username.$invalid && !loginform.username.$pristine }">
<input type="email" name="username" class="form-control" ng-model="user.username" placeholder="Email" required>
<p ng-show="loginform.username.$touched && loginform.username.$invalid" class="help-block">The name is required.</p>
<p ng-show="loginform.username.$touched && loginform.username.$error.email" class="help-block">Please enter valid email id.</p>
</div>
<input type="password" ng-model="user.password" placeholder="Password" class="form-control"> <br>
<button type="submit" value="Log in" class="btn btn-primary" >Log In</button>
</form>

Form data to be fetched and stored in a json string variable using angular and NO-SQL

Here is my registration form and I want all the form fields to be stored in a json string variable using angular and then post that json string to a certain url. When the user is registered it must show "successfully registered" message but if email_id or username already exist server should send back form data in with error. Below is registration form.
<form name="myForm" class="register--form" ng-submit="register()" novalidate>
<fieldset>
<input class="register--first-input" type="text" name="firstname" placeholder="First Name" ng-model="user.firstname" ng-required="true">
<p class="error validationerror" ng-show="myForm.firstname.$invalid && myForm.firstname.$touched">You must enter your first name.</p>
<input class="register--last-input" type="text" name="lastname" placeholder="Last Name" ng-model="user.lastname" ng-required="true">
<p class="error validationerror" ng-show="myForm.lastname.$invalid && myForm.lastname.$touched">You must enter your last name.</p>
<input class="register--email-input" type="email" name="email" placeholder="Email" ng-model="user.email" ng-required="true">
<p class="error validationerror" ng-show="myForm.email.$invalid && myForm.email.$touched">Must be a valid email.</p>
<input class="register--password-input" type="password" name="password" placeholder="Password" ng-model="user.password" ng-required="true">
<p class="error validationerror" ng-show="myForm.password.$invalid && myForm.password.$touched">You must enter a password.</p>
</fieldset>
<button type="submit" class="register--submit" ng-disabled="myForm.$invalid">Register</button>
<span>or</span>
<a class="login--register" ng-href="#/login">Login</a>
</form>
Below is login form
<form name="form" ng-submit="vm.login()" role="form">
<div class="form-group" ng-class="{ 'has-error': form.username.$dirty && form.username.$error.required }">
<label for="username">Username</label>
<input type="text" name="username" id="username" class="form-control" ng-model="vm.username" required />
<span ng-show="form.username.$dirty && form.username.$error.required" class="help-block">Username is required</span>
</div>
<div class="form-group" ng-class="{ 'has-error': form.password.$dirty && form.password.$error.required }">
<label for="password">Password</label>
<input type="password" name="password" id="password" class="form-control" ng-model="vm.password" required />
<span ng-show="form.password.$dirty && form.password.$error.required" class="help-block">Password is required</span>
</div>
<div class="form-actions">
<button type="submit" ng-disabled="form.$invalid" class="btn btn-primary">Login</button>
Register
</div>
</form>
In the controller where you have the button clicked method create a variable like and it will have the values in the key named same as the ng modle
$scope.show={};

Angular form validation $pristine not working properly

Can anyone tell me why my validation is being ignored?
Here is my form:
<form name="contactForm" role="form" ng-submit="controller.submit()" novalidate>
<div class="form-group" ng-class="{ 'has-error' : contactForm.fullName.$invalid && !contactForm.fullName.$pristine }">
<input class="form-control" type="text" name="fullName" placeholder="Full name" ng-model="controller.model.fullName" required />
</div>
<div class="form-group" ng-class="{ 'has-error' : contactForm.email.$invalid && !contactForm.email.$pristine }">
<input class="form-control" type="text" name="email" placeholder="Email address" ng-model="controller.model.email" required />
</div>
<div class="form-group" ng-class="{ 'has-error' : contactForm.phoneNumber.$invalid && !contactForm.phoneNumber.$pristine }">
<input class="form-control" type="text" name="phoneNumber" placeholder="Phone number" ng-model="controller.model.phoneNumber" required />
</div>
<div class="form-group" ng-class="{ 'has-error' : contactForm.orderQuantity.$invalid && !contactForm.orderQuantity.$pristine }">
<select class="form-control" name="orderQuantity" ng-model="controller.model.orderQuantity">
<option disabled selected>Order quantity</option>
<option>10+</option>
<option>20+</option>
<option>30+</option>
<option>40+</option>
</select>
</div>
<div class="form-group" ng-class="{ 'has-error' : contactForm.country.$invalid && !contactForm.country.$pristine }">
<input class="form-control" type="text" name="country" placeholder="Country" ng-model="controller.model.country" required />
</div>
<div class="form-group">
<textarea class="form-control" placeholder="Message" ng-model="controller.model.message"></textarea>
</div>
<div class="form-group">
<div class="recaptcha" theme="dark" vc-recaptcha key="'6Lcc0AgTAAAAAIpcEqqDI3Ko8dZ05H-GGgUnfOvA'"></div>
</div>
<div class="form-group">
<button class="btn btn-primary">Send</button>
</div>
</form>
I set up a codepen here:
http://codepen.io/r3plica/pen/XbzyzQ?editors=101
you should check whether form is submitted or not using contactForm.$submitted.
After form submission the formController object is get updated and various parameter of each controll's model are get updated and then you can validate your data.
you have not checked this in your form validation.
The updated html is
<div class="container" ng-app="validationExample">
<div class="row" ng-controller="ValidationController as controller">
<form style="margin-top: 20px;" name="contactForm" role="form" ng-submit="controller.submit()" novalidate>
<div class="form-group" ng-class="{ 'has-error' : contactForm.fullName.$invalid && contactForm.$submitted }">
<input class="form-control" type="text" name="fullName" placeholder="Full name" ng-model="controller.model.fullName" required />
</div>
<div class="form-group" ng-class="{ 'has-error' : contactForm.email.$invalid && contactForm.$submitted }">
<input class="form-control" type="email" name="email" placeholder="Email address" ng-model="controller.model.email" required />
</div>
<div class="form-group" ng-class="{ 'has-error' : contactForm.phoneNumber.$invalid && contactForm.$submitted }">
<input class="form-control" type="tel" name="phoneNumber" placeholder="Phone number" ng-model="controller.model.phoneNumber" required />
</div>
<div class="form-group" ng-class="{ 'has-error' : contactForm.$submitted && contactForm.orderQuantity.$invalid && contactForm.orderQuantity.$error.required }">
<select class="form-control" required name="orderQuantity" ng-model="controller.model.orderQuantity">
<option disabled selected>Order quantity</option>
<option>10+</option>
<option>20+</option>
<option>30+</option>
<option>40+</option>
</select>
</div>
<div class="form-group" ng-class="{ 'has-error' : contactForm.country.$invalid && contactForm.$submitted}">
<input class="form-control" type="text" name="country" placeholder="Country" ng-model="controller.model.country" required />
</div>
<div class="form-group">
<textarea class="form-control" placeholder="Message" ng-model="controller.model.message"></textarea>
</div>
<div class="form-group">
<button class="btn btn-primary" type="submit">Send</button>
</div>
</form>
</div>
</div>
Here is the updated code codepen
Also instead of $invalid you can use required to validate the fields.Also you can use regular expression to put your custom validation rule.
e.g.
<input name="first_name" class="form-control" required type="text" ng-model="NewUser.first_name" ng-pattern="/^[A-Za-z]+[0-9]*$/" />
<span ng-show="newuser.first_name.$error.pattern">This is not valid <b>Last name</b></span>
Actually the validation is not ignored. In your codepen code, input whatever text and remove to empty. Then switch to another input element. The has-error class is added.
Because you are checking through $dirty, which is by default false if you do not alter any input text (https://docs.angularjs.org/api/ng/type/form.FormController):
$dirty boolean
True if user has already interacted with the form.
I added ng-minlength=5 to your code: http://codepen.io/anon/pen/xGPmqR:
<div class="form-group" ng-class="{ 'has-error' : contactForm.fullName.$invalid && contactForm.fullName.$dirty }">
<input class="form-control" type="text" name="fullName" placeholder="Full name" ng-model="controller.model.fullName" required ng-minlength=5 />
</div>
As you input any text, the ng-minlength rules is working.

Defined function not being called

I have a form
<form role="form" name="signup" novalidate>
<div class="form-group" ng-class="{ 'has-error' : signup.firstname.$invalid && !signup.firstname.$pristine }">
<label>First Name</label>
<input type="text" name="firstname" class="form-control" ng-model="firstname" required>
<p ng-show="signup.firstname.$invalid && !signup.firstname.$pristine" class="help-block">You name is required.</p>
</div><div class="form-group"">
<label>Last Name</label>
<input type="text" name="lastname" class="form-control" ng-model="lastname">
</div>
<div class="form-group" ng-class="{ 'has-error' : signup.phone.$invalid && !signup.phone.$pristine }">
<label>Phone</label>
<input type="text" name="phone" class="form-control" ng-model="phone" ng-minlength="10">
<p ng-show="signup.phone.$invalid && !signup.phone.$pristine" class="help-block">Number is too short!!</p>
</div>
<div class="form-group" ng-class="{ 'has-error' : signup.email.$invalid && !signup.email.$pristine }">
<label>Email</label>
<input type="email" name="email" class="form-control" ng-model="email">
<p ng-show="signup.email.$invalid && !signup.email.$pristine" class="help-block">Enter a valid email.</p>
</div>
<div class="form-group" ng-class="{ 'has-error' : signup.password.$invalid && !signup.password.$pristine }">
<label>Password</label>
<input type="password" name="password" class="form-control" ng-model="password" ng-minlength="6">
<p ng-show="signup.password.$invalid && !signup.password.$pristine" class="help-block">Password is too short</p>
</div>
<div class="form-group" ng-class="{ 'has-error' : signup.passwordRepeat.$invalid && !signup.passwordRepeat.$pristine }">
<label>Password Repeat</label>
<input type="password" name="passwordRepeat" class="form-control" ng-model="passwordRepeat" ng-minlength="6">
<p ng-show="signup.passwordRepeat.$invalid && !signup.passwordRepeat.$pristine" class="help-block">Password repeat is too short</p>
</div>
<button type="submit" class="btn btn-primary btn-success" ng-disabled="signup.$invalid" ng-click="signing()">Create New Account</button>
</form>
it calls a controller function signing()
$scope.signing = function () { $scope.signup($scope.firstname, $scope.lastname, $scope.email, $scope.telephone, $scope.password, $scope.$passwordRepeat); }
which in turn calls
$scope.signup = function ($firstname, $lastname, $email, $phone, $password, $passwordRepeat) {....}
but the console is saying
"Error: $scope.signup is not a function"
the thing is, both functions are defined in the same controller, and it was working fine, before, i can't seem to wrap my head around what could be wrong.
It started saying there was an error, wen i tried to do form validation.
Any help will be highly appreciated.
The problem is that you are using the same name for the form: name="signup". This is also creating a signup property in the $scope, overriding the signup function you defined in the controller. Change one of them at it should work.

Angular form Validation messages not appearing

I'm missing something simple I am sure of it, but my form validation messages never appear in the following code.
<form name="loginform" ng-controller="controllers.LoginController" ng-submit="loginUser(loginform.$valid)" novalidate>
<fieldset>
<br/>
<legend><h3>Account Login</h3></legend>
<div class="{ 'has-error' : loginform.username.$invalid && !loginform.username.$pristine }">
<label>Username:</label>
<input type="text" ng-model="username" name="username" placeholder="username" class="form-control" required><span ng-show="loginform.username.$invalid && !loginform.username.$pristine" class="help-block">Required!</span>
</div>
<br/>
<div class="{ 'has-error' : loginform.password.$invalid && !loginform.password.$pristine }">
<label>Password:</label>
<input type="password" name="password" ng-model="password" placeholder="password" class="form-control" required><span ng-show="loginform.password.$invalid && !loginform.password.$pristine" class="help-block">Required!</span>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" >Submit</button>
Login
</div>
</fieldset>
</form>
controllers.LoginController = function ($scope, $location, AuthFactory, Page) {
'use strict';
$scope.username = null;
$scope.password = null;
$scope.loginUser = function (isValid) {
The $pristine check was the problem... Here are the corrections...
<form name="loginform" ng-submit="loginUser(loginform.$valid)" novalidate>
<fieldset>
<br/>
<legend><h3>Account Login</h3></legend>
<div class="form-group" ng-class="{ 'has-error' : loginform.password.$invalid && submitted }">
<label>Username:</label>
<input type="text" ng-model="username" name="username" placeholder="username" class="form-control" ng-required="true" required><span ng-show="loginform.username.$invalid && submitted" class="help-block">Required!</span>
</div>
<br/>
<div class="form-group" ng-class="{ 'has-error' : loginform.password.$invalid && submitted }">
<label>Password:</label>
<input type="password" name="password" ng-model="password" placeholder="password" class="form-control" required><span ng-show="loginform.password.$invalid && submitted" class="help-block">Required!</span>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" >Submit</button>
Login
</div>
</fieldset>
</form>
If your application is form heavy, consider using this thirdparty ngValidate module.
<input name="demo-field-1" ng-model="user.name" ng-validate="custom-strategy">
The directive will add a span to hold your error messages. You can define custom validation strategies and individual error messages.
demo plnkr

Resources