13

I want to make show/hide password input in angularjs with Bootstrap 3.

The toggle option needs to be inside the input.

Can you provide a snippet or example on that.

Thanks a million.

Sample of it

10 Answers 10

32

You can dynamically change the input type with the ng-attr-type directive. For example:

<input ng-attr-type="{{ showPassword ? 'text' : 'password' }}">

you can tie the showPassword to the click event and make it toggle.

Update (from @Ferie's comment):

HTML

<span class="showPassword" ng-click="togglePassword()">{{ typePassword ? 'Hide' : 'Show' }}</span><br> <input type="{{ typePassword ? 'text' : 'password' }}" name="password" placeholder="Password"> 

In the Angular Controller

$scope.togglePassword = function () { $scope.typePassword = !$scope.typePassword; };
2
  • 1
    to be 100% valid HTML, you'll need to add the type="password" as well, but at angular runtime this will be replaced. Just a tip for anyone that has any kind of automatic HTML validation tooling.
    – Chris Barr
    Commented Mar 10, 2017 at 19:05
  • 3
    HTML <span class="showPassword" ng-click="togglePassword()">{{ typePassword ? 'Hide' : 'Show' }}</span><br> <input type="{{ typePassword ? 'text' : 'password' }}" name="password" placeholder="Password"> In the Angular Controller $scope.togglePassword = function () { $scope.typePassword = !$scope.typePassword; };
    – Ferie
    Commented Apr 21, 2017 at 16:49
8

AngularJS / UI Bootstrap Solution

  • Use Bootstrap's has-feedback class to show an icon inside the input field.
  • Make sure the icon has style="cursor: pointer; pointer-events: all;"
  • Use ng-if to show/hide different forms where the <label type="password"> vs <label type="text">

HTML

<!-- index.html snippet -->    

<!-- show password as type="password" -->
<div ng-if="!showPassword"
       class="form-group has-feedback">
    <label>password</label>
    <input type="password"
           ng-model="params.password"
           class="form-control"
           placeholder="type something here...">
    <span ng-if="params.password"
          ng-click="toggleShowPassword()"
          class="glyphicon glyphicon-eye-open form-control-feedback" 
          style="cursor: pointer; pointer-events: all;">
    </span>
  </div>

  <!-- show password as type="text" -->
  <div ng-if="showPassword"
       class="form-group has-feedback">
    <label>password</label>
    <input type="text"
           ng-model="params.password"
           class="form-control"
           placeholder="type something here...">
    <span ng-if="params.password"
          ng-click="toggleShowPassword()"
          class="glyphicon glyphicon-eye-close form-control-feedback" 
          style="cursor: pointer; pointer-events: all;">
    </span>
  </div>

JavaScript

// app.js

var app = angular.module('plunker', ['ui.bootstrap']);

app.controller('MainCtrl', function($scope) {
    $scope.params = {};
    $scope.showPassword = false;
    $scope.toggleShowPassword = function() {
        $scope.showPassword = !$scope.showPassword;
    }
});

Give it as spin with the plunker: http://plnkr.co/edit/oCEfTa?p=preview

2
  • Why do u use: $scope.params = {}; ?
    – grep
    Commented Nov 17, 2016 at 8:56
  • @grep $scope.params is just an object for holding variables. $scope.params.password gets set by angular's ng-model="params.password" in the html. You'd probably have a login or sign up button that triggers some function that would access the variables in $scope.params. Commented Nov 18, 2016 at 17:33
7

Here's a working demo:

var app = angular.module('myApp', []);
app.controller('loginCtrl', function($scope) {
  $scope.showPassword = false;

  $scope.toggleShowPassword = function() {
    $scope.showPassword = !$scope.showPassword;
  }

});
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<form name="form" ng-app="myApp" ng-controller="loginCtrl" novalidate>
  <label>
    Password:

    <input type="password" name="password" ng-model="password" ng-attr-type="{{ showPassword ? 'text':'password'}}" />
    <div ng-click="toggleShowPassword()" ng-class="{'fa fa-eye': showPassword,'fa fa-eye-slash': !showPassword}" style="cursor: pointer;"></div>

  </label>
</form>

6

you can simply do

<input ng-show="showpassword" type="text" ng-model="password">
<input ng-hide="showpassword" type="password" ng-model="password">
<input type="checkbox" ng-model="showpassword" ng-checked="false">

read here

2
  • Thank you for your response, I have tried this but I have like to have what I have added in edited answer how can make it out from this.
    – Karthik
    Commented Mar 24, 2015 at 4:24
  • To round out this excellent answer, here's the Bootstrap info requested: getbootstrap.com/components/#input-groups Use an input-group-addon.
    – Stone
    Commented Sep 22, 2015 at 0:05
4

You can also try this one

<input type="{{showPassword?'text':'password'}}" />
<input type="checkbox" title="Show Password" ng-model="showPassword" ng-checked="showPassword">
2

Using a unique input and toggle his type like this:

<input type="{{inputType}}" placeholder="Put your password" />

Controller

  // Set the default value of inputType
  $scope.inputType = 'password';      
  // Hide & show password function
  $scope.hideShowPassword = function(){
    if ($scope.inputType == 'password')
      $scope.inputType = 'text';
    else
      $scope.inputType = 'password';
  };

http://codepen.io/gymbry/pen/fJchw/

0

The simple and smart way is

<div class="input-group">
  <input tabindex="2" type="password" class="form-control input-lg" ng-model="_password" placeholder="password" name="_password" required  ng-attr-type="{{ pwd_hide ? 'text':'password'}}">
  <span class="input-group-addon" ng-click="pwd_hide = !pwd_hide">{{ pwd_hide ? 'Hide':'Show'}}</span>
</div>
<span ng-show="submitted || loginForm._password.$dirty && loginForm._password.$invalid">
  <span ng-show="loginForm._password.$error.required" class="error">Please enter your password.</span>
</span>
0
<input class="form-control" type="{{passwordType}}" ng-model="User.Password" name="txtPassword" ng-class="submitted?'ng-dirty':''" autofocus required /><i style="position: relative;float: right;top: -26px;right: 3px; cursor:pointer" ng-click="showPassword()" class="ti ti-eye"></i>

App.controller('myCtrl', function ($scope) { $scope.passwordType = "password";

$scope.showPassword = function () {

        if ($scope.passwordType == "password") {
            $scope.passwordType = "text";
        } else {
            $scope.passwordType = "password";
        }
    }
});
1
  • 1
    Welcome to SO, please describe your answer and do not only post source code!
    – cSteusloff
    Commented Apr 24, 2018 at 12:23
0

you should try to this one whit eye icons

<div class="col-md-6" ng-init="paninptype='password'; iconimg='/images/icons8-invisible-32.png';">
                            <label>PAN Number</label>
                            <div class="input-group">
                                <input type="{{paninptype}}" class="form-control" name="PAN" ng-model="vm.step1.model.PAN" id="exp-pan" placeholder="e.g. FARPS XXXX G" />
                                <span class="input-group-addon">
                                    <img src="{{iconimg}}" class="pull-right" style="width:38px" ng-click="paninptype=(paninptype=='password'?'text':'password'); iconimg=(paninptype=='password'?'/images/icons8-invisible-32.png':'/images/icons8-eye-50.png');">
                                </span>
                            </div>
                        </div>
-1

You want the input box to be hidden or show/hide password?

Here is what i have done till now

HTML

<div class="btn-group">
    <input ng-hide="hidevar" id="searchinput" type="password" class="form-control" placeholder="password" >
      <span id="searchclear" ng-click="hideinpbox();">HIDE</span>
</div>

CSS :

#searchinput {
    width: 200px;
}

#searchclear {
    position:absolute;
    right:5px;
    top:0;
    bottom:0;
    height:14px;
    margin-top:7px;
    margin-right:5px;
    font-size:14px;
    cursor:pointer;
    color:#ccc;
}

http://plnkr.co/edit/TfKvlh1mM6wsNrPQKY4Q?p=preview

Not the answer you're looking for? Browse other questions tagged or ask your own question.