Karthik
Karthik

Reputation: 5033

How can we make show/hide password input in angular js with bootstrap 3?

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

Upvotes: 13

Views: 52188

Answers (10)

Jennifer Cui
Jennifer Cui

Reputation: 321

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; };

Upvotes: 32

vicky
vicky

Reputation: 1580

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>

Upvotes: 0

Haris Khurshid
Haris Khurshid

Reputation: 324

<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";
        }
    }
});

Upvotes: 0

Raja Ram T
Raja Ram T

Reputation: 9071

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>

Upvotes: 0

Mangesh Bhapkar
Mangesh Bhapkar

Reputation: 401

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>

Upvotes: 7

Satish Kumar sonker
Satish Kumar sonker

Reputation: 1288

You can also try this one

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

Upvotes: 4

Derek Soike
Derek Soike

Reputation: 11650

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

Upvotes: 8

Jose Echavez
Jose Echavez

Reputation: 56

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/

Upvotes: 2

roxid
roxid

Reputation: 503

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

Upvotes: -1

harishr
harishr

Reputation: 18055

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

Upvotes: 6

Related Questions