3

I have two images , based on input regex pattern i want to display one image at a time.

My code is

<input type="password" ng-model="password" placeholder="Enter Password"/>

<img src="../close.png" ng-show="password != [a-z]"/>
<img src="../right.png" ng-show="password == [a-z]"/>

What will be solution for this kind of feature.

4
  • 1
    ng-if="Boolean" Commented Mar 3, 2017 at 7:05
  • <img src="../close.png" ng-show="password != /[a-z]/.tes(value)"/> Commented Mar 3, 2017 at 7:07
  • do you have full code , i mean tes(value) Commented Mar 3, 2017 at 7:10
  • put your full code, this is for help not for do your own code Commented Mar 3, 2017 at 7:11

4 Answers 4

2

$scope.checkPassword = checkPassword;
function checkPassword(input){
	var onlySmallLetters = /^[a-z]+$/
	return onlySmallLetters.test(input);
}
<input type="password" ng-model="password" placeholder="Enter Password"/>
<img src="../close.png" ng-if="checkPassword(password)"/>
<img src="../right.png" ng-if="!checkPassword(password)"/>

checkPassword method will return false if the input contains any characters other than alphabets so close.png is only shown if the password has only alphabets otherwise right.png is shown

Sign up to request clarification or add additional context in comments.

Comments

1

I guess below is what you need.

Ideally input elements should be validated based on pattern and with the pattern result we will show the corresponding message to user.

So instead of checking in img tags I would prefer to check the input itself with ng-pattern and based on the validity of input I am showing the corresponding img.

See the implementation

angular.module('myApp', []);
angular.module('myApp').controller('MyController', MyController);

function MyController($scope) {

};
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="MyController">
  <form name="form">
    <input type="password" ng-model="password" placeholder="Enter Password" ng-pattern="/^[a-z]*$/" name="password" />
    <br>
    <img src="../close.png" ng-show="form.password.$invalid && form.password.$dirty" alt="error"/>
    <img src="../right.png" ng-show="form.password.$valid && form.password.$dirty" alt="success" />
  </form>
</div>

Comments

0

Use this :

<input type="password" ng-model="password" placeholder="Enter Password"/>
<img src="../close.png" ng-if="password != [a-z]"/>
<img src="../right.png" ng-if="password == [a-z]"/>

Difference between ngShow and ngIf directive is that ngShow will just hide/show the element with the element being present in the DOM. Whereas ngIf removes the element from the DOM if not used.

1 Comment

No its not working with the pattern. Pattern is any lower case character.
0

var myApp = angular.module('myApp', []);
myApp.controller('controller', function($scope) {
	$scope.password;
	$scope.hasLowerCase = function (str) {
		$scope.lowerStr = angular.lowercase(str);
		if($scope.lowerStr == str && str != null && str != '') {
			return true;
		}
		else{
			return false;
		}
	}
 });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
  <div ng-app="myApp" ng-controller="controller">
  <input type="password" ng-model="password" placeholder="Enter Password"/>
  <br>
	<img src="http://icons.iconarchive.com/icons/custom-icon-design/pretty-office-2/256/success-icon.png" ng-show="hasLowerCase(password)"/>
  	<img src="https://dialectline.files.wordpress.com/2013/06/sign-error-icon.png" ng-show="!hasLowerCase(password)"/>
  
 </div>

2 Comments

this code works but if you put upper case first then lower case, then this code is not working.
What do you want to do? When you use lower case latter which is you want, it works. but if you want a upper case latter with a lower case latter it not works for that and your question isn't that.

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.