0

I work on my angularjs projerct.

I have button in my template and two input text elements.

The button have to be enabled only when two input boxes has some string.

Here is HTML code:

    <form id="loginform" class="form-horizontal" role="form">
        <div style="margin-bottom: 25px" class="input-group">
            <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
            <input id="login-username" type="text" class="form-control" ng-model="UserName" placeholder="UserName">
        </div>
        <div style="margin-bottom: 25px" class="input-group">
            <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
            <input id="login-password" type="text" class="form-control" ng-model="Password" placeholder="Password">
        </div>

        <div style="margin-top:10px" class="form-group pull-left">
            <div class="col-sm-12 controls">
                <input type="button" 
                       class="btn btn-success" 
                       value="enter" 
                       ng-click="inspectorArea.verify(UserName,Password)" 
                       ng-disabled="(UserName == null || Password == null)">
            </div>
        </div>
    </form>

and here is working plunker.

Using this row ng-disabled="(UserName == null || Password == null) in input button element I disable or enable button.

But it not working properly, you can see from plunker-example, button is enabled only when text boxes has string only for first time, if I remove string from one of the texts box elements the button is enable while I want them to be disalbled.

Any idea how can I disable button if at least one of the text boxes is empty?

1
  • Please have a look at my answer below. There is a very minor change required. Commented Nov 5, 2016 at 15:49

4 Answers 4

2

Initially UserName and Password are null. So it will work. But when you type something and delete, they become empty and that's why it didn't work.

I have modified it to check the truthiness.

<input type="button" 
                       class="btn btn-success" 
                       value="enter" 
                       ng-click="inspectorArea.verify(UserName,Password)" 
                       ng-disabled="(!UserName || !Password)">
Sign up to request clarification or add additional context in comments.

Comments

0

You should use ng-required and $invalid property of input field.

Check this code:

  <form name="loginform" id="loginform" class="form-horizontal" role="form">
      <div style="margin-bottom: 25px" class="input-group">
          <span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
          <input name="uname" ng-required="true" id="login-username" type="text" class="form-control" ng-model="UserName" placeholder="UserName">
      </div>
      <div style="margin-bottom: 25px" class="input-group">
          <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
          <input name="pass" ng-required="true" id="login-password" type="text" class="form-control" ng-model="Password" placeholder="Password">
      </div>

      <div style="margin-top:10px" class="form-group pull-left">
          <div class="col-sm-12 controls">
              <input type="button" 
                     class="btn btn-success" 
                     value="enter" 
                     ng-click="inspectorArea.verify(UserName,Password)" 
                     ng-disabled="loginform.uname.$invalid || loginform.pass.$invalid">
          </div>
      </div>
  </form>

Comments

0

You can just use required on your input elements, no need for ng-required and then use ng-disabled="yourFormName.$invalid" as an attribute on your button element. To do this you must assign name="yourInputNames" to each of your input elements.

Comments

0

Just add an ID to the button and this would work dynamically:

<script>
    $(document).ready(function(){

        $('#the_submit').prop('disabled','true');
        $('#the_submit').attr('title','text boxes are empty');
    });
    $(document).ready(function () {
        $('#login_password').keyup(function(){
            var val = $.trim($("#login_password").val());
            var val2 = $.trim($("#login_username").val());

            if (val != "" && val2 != "") {
            $('#the_submit').removeAttr('disabled');
            $('#the_submit').attr('title','Good To Go');
            }
        });
    });
    $(document).ready(function () {
        $('#login_password').keyup(function(){
            var val = $.trim($("#login_password").val());
            var val2 = $.trim($("#login_username").val());

            if (val == "" || val2 == "") {
            $('#the_submit').prop('disabled','true');
            $('#the_submit').attr('title','Edit Must Be Documented'); 
            }
        });
    });
    </script>";
}

// Here is your button, give it an id
<input type="submit" id="the_submit">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.