Angular.js Form Invalid- Don't know why - angularjs

I don't know why, but by default in Angular.js this for is invalid by dafault(no interaction on the form):
<!-- Persistence -->
<div class="col-sm-6" ng-class="{'has-error': form.persistence.$dirty && form.persistence.$invalid}">
<div class="checkbox">
<input for="persistence" type="checkbox" ng-change="resetPersistenceInputBox(form, vip)" ng-model="persistanceCheckbox" ng-init="persistanceCheckbox=false">Client IP Based Persistence
<div>
<label class="control-label" style="font-weight: bold">Enter CIDR Netmask</label>
<div class="input-group">
<span class="input-group-addon">
x.x.x.x/
</span>
<input type="text" class="col-sm-6 form-control" id="persistence" ng-init="persistanceCheckbox=false" name="persistence"
ng-disabled="!persistanceCheckbox"
ng-required="persistanceCheckbox"
ng-model="vip.algorithm.persistence"
ng-pattern='/^([0-9]|[12]\d|3[0-2])$/'
ng-trim="true"
placeholder="Enter a netmask between 0-32">
</div>
<div class="help-block" ng-show="form.persistence.$dirty && form.persistence.$invalid">
<span ng-show="form.persistence.$error.pattern && form.persistence.$dirty">Netmask must be a number between 0-32.</span>
<span ng-show="form.persistence.$error.required">CIDR Netmask is required while "Client IP Based Persistence" is checked.</span>
</div>
</div>
</div>
</div>

Your form is not valid until you type the input
<input type="text" class="col-sm-6 form-control" id="persistence" ng-init="persistanceCheckbox=false" name="persistence"
ng-disabled="!persistanceCheckbox"
ng-required="persistanceCheckbox"
ng-model="vip.algorithm.persistence"
ng-pattern='/^([0-9]|[12]\d|3[0-2])$/'
ng-trim="true"
placeholder="Enter a netmask between 0-32">
The input is required with ng-required="persistanceCheckbox"
and must meet the ng-pattern : ng-pattern='/^([0-9]|[12]\d|3[0-2])$/'

Related

Ng-click validation is not working

I am beginer in angular js. I am validating a form with some input feild and form is posting on ng-click but validation is not working, validation message are displaying for a white then disappear i have to submit the form after validating. form ng-click should not be called untill the form is valid please help me . Thanks in advance.
<form name="teamForm" novalidate ng-submit="submit(teamForm)" class="formfields">
<div class="col-md-12">
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<label for="lname">First Name:</label>
<input type="text" name="firstname"
ng-model="FirstName" class="form-control custom-form-control"
placeholder="First Name" required="required">
<span class="text-danger"
ng-show="(teamForm.firstname.$dirty || submitted) && teamForm.firstname.$error.required">Required</span>
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="form-group">
<label for="lname">Last Name:</label>
<input type="text" name="lastname"
ng-model="LastName" class="form-control custom-form-control"
placeholder="Last Name" required="required">
<span class="text-danger"
ng-show="(teamForm.lastname.$dirty || submitted) && teamForm.lastname.$error.required">Required</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-6">
<div class="form-group">
<label for="email">Email:</label>
<input type="text" name="email"
ng-model="Email" class="form-control custom-form-control"
ng-pattern="/^[^\s#]+#[^\s#]+\.[^\s#]{2,}$/"
placeholder="Email" required="required">
<span class="text-danger"
ng-show="(teamForm.email.$dirty || submitted) && teamForm.email.$error.required">Required</span>
<span class="text-danger"
ng-show="teamForm.email.$dirty &&teamForm.email.$error.pattern">Please Enter Valid Email</span>
</div>
</div>
<div class="col-md-6 col-sm-6">
<div class="form-group">
<label>Phone Number:</label>
<div class="clearfix"></div>
<input type="text" name="phone"
ng-model="Phone" class="form-control custom-form-control"
placeholder="XXXXXXXXXX" required="required">
<span class="text-danger"
ng-show="(teamForm.phone.$dirty || submitted) && teamForm.phone.$error.required">Required</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 col-sm-12">
<div class="form-group">
<label>Message:</label>
<textarea class="form-control rounded-0" rows="5"
name="comment" placeholder="Message"
ng-model="Comment" required="required"></textarea>
<span class="text-danger"
ng-show="(teamForm.comment.$dirty || submitted) && teamForm.comment.$error.required">Required</span>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 col-sm-12">
<div class="form-group">
<label>Upload Resume:</label>
<!--<input type="file" name="ResumePath" id="filehandler" />-->
<input type="file" id="file1" name="file" class="filelabel sr-only" multiple ng-files="getTheFiles($files)" onchange="Checkfiles($(this))" />
<!-- <input type="file" name="file" onchange="angular.element(this).scope().uploadFile(this.files)"/> -->
<label for="file1" class="form-control">
<span><i class="fa fa-file"></i> Drag file here or choose file</span>
</label>
</div>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<div vc-recaptcha key="'6Lc860IUAAAAAAyWI9WD8EV4eITu4ODdhuYHdzi8'"
class="grecaptcha" ng-model="respone1"></div>
</div>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<button type="button" id="btnSubmit"
ng-click="uploadFiles()" value="Upload"
class="btn btn-green center-block pull-left">
<i class="fa fa-send"></i>{{btnText}}</button>
</div>
</div>
<div class="form-group text-center">
<h5 class="text-success" style="font-weight:bold">{{messagesuccess}}</h5>
<h5 class="text-danger" style="font-weight:bold">{{messageerror}}</h5>
</div>
</div>
</form>
ng-click (or it's vanilla cousin, onclick) do not check form validation. The function for submission needs to be defined at the form level, and then you specify which button acts as the submit button in order to get form behavior.
I see you already have a submit function defined. I assume you want to change that to uploadFiles. And if you want the form to conduct validation, remove the novalidation attribute.
<form name="teamForm" ng-submit="uploadFiles()" class="formfields">
then, for the button you would specify it is the submission button and remove the ng-click.
<button type="submit" id="btnSubmit"
value="Upload"
class="btn btn-green center-block pull-left">
<i class="fa fa-send"></i>{{btnText}}</button>

Angular js $form.valid is not working

I am working with Angular 1.6.4 version in my project.
I am using the following template for create user details.Here I am making some validations for the fields, until it fulfills the validation, save button will not be enabled.
This is working fine, and error messages for validation works fine in creating users.
I am using the same page to update users, while the user details will be populated in textfields, Here the save button is not enabled even though user details have been populated.
Can anyone assist here in this.
Here is my code,
<form name="userForm">
<div class="header-buttons3">
<button type="button" class="header-button-save btn" ng-disabled="userForm.$invalid" ng-click="submitUserForm()"><i class="fa fa-pencil"></i>SAVE</button>
<button type="button" class="header-button-cancel"><i class="fa fa-chevron-left"></i>CANCEL</button>
</div>
<div class="clearfix"></div>
<div class="row">
<div class="col-md-11 col-sm-12 col-xs-12">
<div class="x_content">
<br>
<div class="form-row row">
<div class="form-group required col-md-3">
<label for="first name" class="control-label">FIRST NAME</label>
<span class="after">*</span> <input class="form-control"
ng-class="{validbr: showfn==='false', invalidbr: showfn }"
ng-focus="showfn=false"
ng-blur="showfn=userForm.firstName.$invalid" id="firstname"
class="form-control" ng-model="user.firstName"
ng-pattern="/^[a-zA-Z]*$/" name="firstName" ng-maxlength="30"
maxlength="30" required placeholder="Enter First Name" />
<div style="color: Red"
ng-show="showfn && userForm.firstName.$error.required">Error
- First Name Required</div>
<div style="color: Red"
ng-show="showfn && userForm.firstName.$error.pattern">Error
- Enter only Character</div>
<div style="color: Red"
ng-show="showfn && userForm.firstName.$error.maxlength">Error
- Need 30 char max</div>
</div>
<div class="form-group required col-md-3">
<label for="last name" class="control-label">LAST NAME</label> <span
class="after">*</span> <input class="form-control"
ng-class="{validbr: showln==='false', invalidbr: showln }"
ng-focus="showln=false"
ng-blur="showln=userForm.lastName.$invalid" id="lastname"
ng-model="user.lastName" ng-pattern="/^[a-zA-Z]*$/"
name="lastName" ng-maxlength="50" maxlength="50" required
placeholder="Enter Last Name" />
<div style="color: Red"
ng-show="showln && userForm.lastName.$error.required">Error
- Last Name Required</div>
<div style="color: Red"
ng-show="showln && userForm.lastName.$error.pattern">Error
- Enter only Character</div>
<div style="color: Red"
ng-show="showln && userForm.lastName.$error.maxlength">Error
- Need 50 char max</div>
</div>
<div class="form-group required col-md-3">
<label for="inputEmail4" class="control-label">EMAIL
ADDRESS</label> <span class="after">*</span>
<input type="email" readonly class="form-control"
ng-class="{validbr: showem==='false', invalidbr: showem }"
ng-focus="showem=false"
ng-blur="showem=userForm.emailId.$invalid" id="emailId"
ng-model="user.emailId" name="emailId" ng-maxlength="70"
maxlength="70" required placeholder="Enter Email Address" />
<div style="color: Red"
ng-show="showem && userForm.emailId.$error.required">Error
- Email address Required</div>
<div style="color: Red"
ng-show="showem && userForm.emailId.$error.email">Error -
Not a valid email address</div>
</div>
<div class="form-group required col-md-3">
<label for="inputEmail4" class="control-label">USER STATUS
</label><br/>
<label>
<input type="radio" ng-model="user.active" ng-value="false">
IN ACTIVE
</label>
<label>
<input type="radio" ng-model="user.active" ng-value="true">
ACTIVE
</label>
</div>
</div>
<div class="form-row row">
<div class="form-group col-md-3">
<label for="mobile phone" class="col-form-label">MOBILE
PHONE</label> <input class="form-control" id="mobileNumber"
ng-class="{validbr: showmn==='false', invalidbr: showmn }"
ng-focus="showmn=false"
ng-blur="showmn=userForm.mobileNumber.$invalid"
ng-model="user.mobileNumber" name="mobileNumber"
ng-minlength="14" ng-maxlength="14" phone-input
placeholder="Enter Mobile Phone #" />
<div style="color: Red"
ng-show="showmn && userForm.mobileNumber.$error.minlength ">Error
- Need 10 digits for a Phone #</div>
<div style="color: Red"
ng-show="showmn && userForm.mobileNumber.$error.maxlength ">Error
- Need 10 digits for a Phone #</div>
<!-- <div ng-show="userForm.mobileNumber.$error.minlength">Error - Need 10 digits for a Phone #</div> -->
</div>
<div class="form-group col-md-3">
<label for="Desk Phone" class="col-form-label">DESK PHONE</label>
<input class="form-control" id="deskNumber"
ng-class="{validbr: showdn==='false', invalidbr: showdn }"
ng-focus="showdn=false"
ng-blur="showdn=userForm.deskNumber.$invalid" name="deskNumber"
ng-model="user.deskNumber" ng-minlength="14" maxlength="14"
phone-input placeholder="Enter Desk Phone #" />
<div style="color: Red"
ng-show="showdn && userForm.deskNumber.$error.minlength">Error
- Need 10 digits for a Phone #</div>
<div style="color: Red"
ng-show="showdn && userForm.deskNumber.$error.maxlength">Error
- Need 10 digits for a Phone #</div>
</div>
<div class="form-group">
<!-- <input type ="hidden" ng-model="user.createdBy" name="createdBy" />
<input type ="hidden" ng-model="user.updatedBy" name="updatedBy" /> -->
</div>
<!--
<input type="hidden" ng-value="user.createdBy" name="createdBy" />
<input type="hidden" ng-value="user.updatedBy" name="updatedBy" /> -->
</div>
</div>
</div>
</div>
</form>

tabindex is not working with required attribute

<div class="row">
<div class="col-md-6">
<div class="form-body">
<div class="form-group">
<label>First Name</label>
<div class="input-group">
<input type="text" tabIndex="1" name="first_name" class="form-control">
</div>
</div>
<div class="form-group">
<label>Email Address</label>
<div class="input-group">
<input type="email" tabIndex="3" name="username" class="form-control" ng-model="username" required user-exist /><br/> <span style="color: red" ng-show="partnerForm.username.$touched && partnerForm.username.$invalid">
<span ng-show="partnerForm.username.$error.required">Email is required.</span>
<span ng-show="partnerForm.username.$error.email">Enter Correct Format.</span>
<span ng-show="partnerForm.username.$error.userExist">User already exists.</span>
</span>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-body">
<div class="form-group">
<label>Last Name</label>
<div class="input-group">
<input type="text" tabIndex="2" name="last_name" class="form-control">
</div>
</div>
</div>
</div>
</div>
the tabindex 3 is not working.i have checked by removing the required attr then it is working .the attributes required and tabindex is conflicting with each other.please help me to solve this.or provide a directive (working) for tabindex.

How to switch form base of flag in HTML (Angular JS)

I want to use same form for two different Views and I have two different roles.
1. Client
2. Manager
In my Controller I have added a flag based on the user role that is logged in.
$scope.userFlag=$rootScope.globalSession.UserRole,
If I login as a Manager then the value of $rootScope.globalSession.UserRole="Manager"
& If I login as a Client then the value of $rootScope.globalSession.UserRole="Client"
Now in my form I have added a condition to switch it -> ng-if="userFlag==Admin"
<form class="form-horizontal" name="UpdateAdminProfileForm" id="UpdateAdminProfileForm">
<h2>Update Profile</h2>
<hr>
<fieldset name="client" id="client" ng-if="userFlag==Admin">
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="username">Domain Name*</label>
<div class="col-md-4">
<input id="username" name="username" type="text" placeholder="Enter your username" class="form-control input-md" ng-model="theClient.OrganizationDomain" disabled>
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="update"></label>
<div class="col-md-4">
<a><button id="update" name="update" class="btn btn-primary" ng-click="updateClient()">Update</button></a>
</div>
</div>
</fieldset>
<fieldset name="manager" id="manager" ng-show="userFlag==Manager">
<div class="form-group" ng-class="{ 'has-error': submitted && UpdateAdminProfileForm.myemail.$error.required || UpdateAdminProfileForm.myemail.$error.pattern }">
<label class="col-md-4 control-label" for="myemail">Email*</label>
<div class="col-md-4">
<input id="myemail" name="myemail" type="email" placeholder="Enter your email" class="form-control input-md" ng-model="theClient.Email" ng-pattern="regex.Email" ng-maxlength="20" required autofocus >
<span ng-show="submitted && UpdateAdminProfileForm.myemail.$error.required" class="help-block">Email can not be empty</span>
<span ng-show="UpdateAdminProfileForm.myemail.$error.pattern && UpdateAdminProfileForm.myemail.$invalid " class="help-block">Please enter a valid email</span>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-primary" data-dismiss="modal" type="button" ng-click="updateManager()">Save</button>
</div>
</fieldset>
</form>
</div>
</div>
But its not working when I open the form, its empty and If I remove ng-if="userFlag==Admin" & ng-if="userFlag==Manager" from tags then it display the fields for both field set.
Image With FLAG
Image After Removing FLAG
Help! Thanks in advance.
It should be
ng-if="userFlag=='Admin'"
Try This
ng-if="userFlag=='Admin'" or ng-show="userFlag=='Manager'"

how to display all error messages in the form header in angularjs

I need to display all the messages at the top of the form and I need to implement the custom error for fields like email and phone number.
index.html
<form name="adminForm" ng-submit="addAdmin()" novalidate>
all error messages need to be displayed here
<div class="form-group" ng-class="{ 'has-error': adminForm.firstName.$touched && adminForm.firstName.$invalid }">
<label>First Name</label>
<input type="text" name="firstName" class="form-control" ng-model="admin.first_name" ng-maxlength="255" required>
<div class="help-block" ng-messages="adminForm.firstName.$error" ng-if="adminForm.firstName.$touched">
<div ng-messages-include="messages.html"></div>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error': adminForm.lastName.$touched && adminForm.lastName.$invalid }">
<label>Last Name</label>
<input type="text" name="lastName" class="form-control" ng-model="admin.last_name" ng-maxlength="255" required>
<div class="help-block" ng-messages="adminForm.lastName.$error" ng-if="adminForm.lastName.$touched">
<div ng-messages-include="messages.html"></div>
</div>
</div>
<div class="form-group" ng-class="{ 'has-error': adminForm.email.$touched && adminForm.email.$invalid }">
<label>Email</label>
<input type="email" name="email" class="form-control" ng-model="admin.email" ng-maxlength="255" required>
<div class="help-block" ng-messages="adminForm.email.$error" ng-if="adminForm.email.$touched">
<div ng-messages-include="messages.html"></div>
</div>
</div>
messages.html
<p ng-message="required">This field is required</p>
<p ng-message="minlength">This field is too short</p>
<p ng-message="maxlength">This field is too long</p>
<p ng-message="email">This needs to be a valid email</p>
<p ng-message="pattern">This needs to be a valid number</p>
<p ng-message="equals">Password must be same</p>
I'm looking for any possible solution or a reference to other possible solutions.
Reason : I'm finding difficulty in implementing unit tests for this(noob), so i think a single error message can make it easy for testing.
Thanks!

Resources