Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable a button if at least one input field is empty using ngDisabled

Tags:

angularjs

I'd like to prevent the user from clicking the button "Register" if the fields are not correct. Part of that is that certain fields must be filled. But the condition is starting to get a bit long:

<button class="btn btn-success" ng-click="register()"
    ng-disabled="signInForm.$invalid || form.firstName.length==0 ||
    form.lastName.length==0 || form.email.length==0 ||
    form.password.length==0 || form.passwordBis.length==0">Register</button>

How can I simplify that?

like image 562
Elfayer Avatar asked Oct 02 '14 13:10

Elfayer


People also ask

How do I disable a button if input is empty?

Just click f12 in your browser, find the submit button in the html, and then remove the disabled ! It will submit the form even if the inputs are empty.

How do I disable the button if the input box is empty and enable when field is filled in angular?

When using AngularJS Form validation, the $invalid property can be used to disable the Submit Button when Form is not filled (empty) i.e. the INPUT Text Fields (TextBoxes) have invalid data. The below HTML Markup consists of an HTML DIV to which ng-app and ng-controller AngularJS directives have been assigned.


1 Answers

I think what you need is to provide required attribute for your input fields inside your form, so until the fields are filled in the form signInForm will be invalid. Similarly you could provide other validation attributes on the inputs as well.

Example:-

  <input type="text" ng-model="firstName" name="firstName" required />
  <input type="text" ng-model="lastName" name="lastName" required />
  <input type="email" ng-model="email" name="email" required />
  ...
  ...
  <button class="btn btn-success" ng-click="register()" 
            ng-disabled="signInForm.$invalid">Register</button>
like image 54
PSL Avatar answered Oct 18 '22 20:10

PSL