I've created a form with angularjs, but when I first load the page, the form starts out invalid and the entire form turns red. I've added "novalidate" to the form and "required" to an input, yet the whole form starts our invalid and red.
From this program when you are clicking submit button without entering any values validation will happen
<form name="product_form" class="form-horizontal" role="form" novalidate>
<div class="control-group" ng-class="{true: 'error'}[submitted && product_form.emailid.$invalid]">
<form-element label="Name" mod="product">
<input type="text" class="form-control" name="name" placeholder="name" ng-model="product.name" ng-disabled="product.id" required/>
<span class="help-block" ng-show="submitted && product_form.name.$error.required"> Please enter name </span>
</form-element>
<form-element label="Emailid" mod="product">
<input type="email" name="emailid" class="form-control" placeholder="emailid" ng-model="product.emailid" required>
<span ng-show="submitted && product_form.emailid.$error.required" class="help-block">Please enter emailid</span>
</form-element>
<form-element label="Password" mod="product">
<input type="password" name="password" class="form-control" placeholder="password" ng-model="product.password" required/>
<span ng-show="submitted && product_form.password.$error.required" class="help-block"> Please enter password </span>
</form-element>
</div>
<div class="space"></div>
<div class="space-4"></div>
<div class="modal-footer">
<a class="btn btn-sm" ng-click="cancel()" ><i class="ace-icon fa fa-reply"></i> Cancel </a>
<button ng-click="saveProduct(product);submitted=true"
ng-enabled="product_form.$invalid || enableUpdate"
class="btn btn-sm btn-primary ng-binding"
type="submit">
<i class="ace-icon fa fa-check"></i>Submit
</button>
</div>
</form>
Related
Hi, I got a problem I want to show the messages only when passwords match or not match but the messages appear all the time any suggestions? And I would like to not be able to click the button if the passwords are different!
#*<form name="form" ng-submit="vm.login()" role="form">*#
<form accept-charset="UTF-8" role="form" id="login-recordar" ng-submit="vm.login()">
#*New password field!*#
<div>
<div class="form-group input-group" ng-class="{ 'has-error': form.password.$dirty && form.password.$error.required }">
<span class="input-group-addon">
<i class="glyphicon glyphicon-lock"></i><label for="password1">New Password</label>
</span>
<input class="form-control" placeholder="Password" name="password" type="password" data-toggle="popover" data-trigger="focus" data-content="Minimum 8 characters long and should contain at least one (small- and capital letter and number)." id="password" value="" required="" autofocus="" onkeypress="capLock(event)" ng-model="mAddEditView.User.Password" ng-pattern="/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,})/" />
</div>
<span ng-show="form.password.$dirty && form.password.$error.required" class="help-block">Password is required</span>
<span id="divMayus" style="visibility:hidden">Caps Lock is on!</span>
</div>
#*Confirm Password field!*#
<div>
<div class="form-group input-group" ng-class="{ 'has-error': form.password1.$dirty && form.password1.$error.required }">
<span class="input-group-addon">
<i class="glyphicon glyphicon-lock"></i><label for="password1">Confirm Password</label>
</span>
<input class="form-control" placeholder="Password" name="password1" type="password" data-toggle="popover" data-trigger="focus" data-content="The password should match." id="password1" value="" required="" onkeypress="capLock(event)" ng-model="mAddEditView.User.Password" ng-pattern="/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.{8,})/" />
</div>
<span ng-show="form.password1.$dirty && form.password1.$error.required" class="help-block">Password is required</span>
<span ng-show="mAddEditView.User.Password !== mAddEditView.User.Password1" class="help-block"><font style="color:red;">Password is not valid!</font></span>
#* code for checking if password match *#
<span ng-show="form.password1.$valid && mAddEditView.User.Password === mAddEditView.User.Password1"><font style="color:white;">Password Matched</font></span>
<span id="divMayus" style="visibility:hidden">Caps Lock is on!</span>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-block" value="Login" ng-disabled="form.$invalid || vm.dataLoading || errors.mail.isExists || mAddEditView.User.Password !== mAddEditView.User.Password1">
Change Password
</button>
</div>
You can update your ng-show condition for message to
ng-show="form.password1.$valid && form.password1.$dirty && form.password.$dirty && mAddEditView.User.Password === mAddEditView.User.Password1"
So that it'll show only when user started editing both fields & user added valid password according to RegExp & then last condition to check if both are same.
*Required validation is necessary for making form invalid & avoiding user to submit it. So adding required attribute & for submit button form.$invalid will handle it itself
You should also check whether password1 is passing required validation or not.
ng-show="!form.password1.$error.required &&
form.password1.$valid &&
mAddEditView.User.Password === mAddEditView.User.Password1"
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>
I want to apply Angular Form Validation without using an HTML form. I tried following, but it doesn't disable the button.
<ng-form name="login">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="text" name="username" value="" placeholder="Username" class="form-control" id="login-username" ng-required="true">
</div>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input type="password" id="login-password" class="form-control" name="password" placeholder="Password" ng-required="true">
</div>
<button style="width: 100%;" title="" data-original-title="" role="button" class="btn custom-small-btn btn-primary hvr-float-shadow" ng-disabled="login.username.$invalid || login.password.$invalid">Login</button>
</ng-form>
tried using ng-diabled="login.$invalid" as well and that didn't work either. What should I do to make it work.
Just use ng-model in your form fields and it will work::
<ng-form name="login">
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-user"></i></span>
<input type="text" ng-model="username" name="username" value="" placeholder="Username" class="form-control" id="login-username" ng-required="true">
</div>
<div class="input-group">
<span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input type="password" ng-model="password" id="login-password" class="form-control" name="password" placeholder="Password" ng-required="true">
</div>
<button style="width: 100%;" title="" data-original-title="" role="button" class="btn custom-small-btn btn-primary hvr-float-shadow" ng-disabled="login.username.$invalid || login.password.$invalid">Login</button>
</ng-form>
<form class="stdform stdform2" name="projectForm" ng-submit="saveProject(project, projectForm)"
novalidate="novalidate">
<p>
<div class="par control-group" ng-class="{'has-error': projectForm.projectName.$invalid && (projectForm.projectName.$dirty || submitted)}">
<label class="control-label" for="projectname">Name</label>
<div class="controls">
<span class="field">
<input id="projectname" type="text" ng-model="project.name" name="projectName" class="form-control"
required/>
<label ng-show="projectForm.projectName.$error.required && (projectForm.projectName.$dirty || submitted)"
class="error" for="projectname">Please type project name</label>
</span>
</div>
</div>
</p>
<p>
<label>Description</label>
<span class="field">
<textarea ng-model="project.description" name="projectDescription"
style="resize: vertical; height: 110px;" class="form-control input-default"></textarea>
</span>
</p>
<p class="stdformbutton">
<button class="btn btn-default" ui-sref="app.project.list">Cancel</button>
<button class="btn btn-primary" type="submit">{{saveMessage}}</button>
</p>
</form>
Why after pressing the 'Enter' key on keyboard am I redirected to app.project.list instead of having the saveProject(project, projectForm) function executed?
The default type for buttons is submit. You have to explicitly unset it to "button" to avoid the submit action:
<p class="stdformbutton">
<button class="btn btn-default" type="button" ui-sref="app.project.list">Cancel</button>
<button class="btn btn-primary" type="submit">{{saveMessage}}</button>
</p>
You can also set the type="reset" if you want it to clear out the input values.
I am displaying a form in a modal. But when I click on the text input field it is not allowing me to enter text. similarly unable to change/select radio buttons
But when trying with "tab" it is working.
code
<form role="form" name="basicDetails" class="form-validation">
<div class="col-sm-6 b-r">
<h3 class="m-t-none m-b font-thin">Basic Details</h3>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" placeholder="Enter Email" ng-model="email" required>
</div>
<div class="form-group">
<label>User Name</label>
<input type="text" class="form-control" placeholder="Enter User Name" ng-model="username" ng-pattern="/^[a-zA-Z ]{1,17}$/" required>
</div>
</div>
<div class="checkbox m-t-lg">
<button type="submit" class="btn btn-sm btn-success pull-right text-uc m-t-n-xs" ng-click="ok()"><strong>UPDATE</strong></button>
<button type="submit" class="btn btn-sm btn-danger pull-right text-uc m-t-n-xs" ng-click="close()"><strong>CLOSE</strong></button>
</div>
</form>
inspect element shows
Can someone help me to trace the issue.