Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 807
Karthik Avatar asked Mar 24 '15 04:03

Karthik


People also ask

How do I make my HTML password not visible?

</div> <div class="col-md-6"> <div class="form-group"> <input type="password" class="form-control" placeholder="Confirm Password" >


3 Answers

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

like image 57
Derek Soike Avatar answered Oct 19 '22 18:10

Derek Soike


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; };
like image 27
Jennifer Cui Avatar answered Oct 19 '22 17:10

Jennifer Cui


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

like image 22
harishr Avatar answered Oct 19 '22 18:10

harishr