2016-11-09 3 views
1

入力タイプのテキストが変更または消去された場合にのみ、コントロールがコードブロックに入るコードを記述しようとしています。私はそれを変更するまで、これは動作しません

if(myfrm.textchecker.$dirty && !myfrm.textchecker.$pristine) 

:私は、以下の条件を使用しています。ここ

if(!myfrm.textchecker.$dirty && !myfrm.textchecker.$pristine)//completely opposite 

参照のための私のコードです:

<html> 
<head> 
<script src="angular.min.js"></script> 
</head> 
<body ng-app="myApp"> 
<div ng-controller="outerCtrl"> 
<form name="myfrm"> 
<input type="password" name="textchecker" ng-change="processed(ngdata)" ng-model="ngdata" required/> 

<input type="submit" name="submit" value="submit"> 
</form> 
<span style="color:red" ng-show="myfrm.textchecker.$error.required">please enter the required text</span> 

{{ngdata}} 
<p> 
{{final}} 

</p> 

<p> 
    $dirty : {{ myfrm.textchecker.$dirty }} </br> 
    $invalid : {{myfrm.textchecker.$invalid }} 
    </br> 
    $pristine : {{myfrm.textchecker.$pristine }} 
    </p> 

</div> 
</body> 
</html> 
<script> 
var app=angular.module("myApp",[]); 
app.controller("outerCtrl",function($scope){ 



$scope.processed=function(password) 
    { 
     if(!myfrm.textchecker.$dirty && !myfrm.textchecker.$pristine) 
     { 
      console.log('!myfrm.textchecker.$dirty + !myfrm.textchecker.$pristine'); 

      console.log(!myfrm.textchecker.$dirty + !myfrm.textchecker.$pristine); 
    // var password=$scope.ngdata; 
     var strength=0; 
     //console.log(password); 
    // alert($scope.ngdata); 
     // if (password.length > 7) strength += 1 

     //if password contains both lower and uppercase characters, increase strength value 
     if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) strength += 1 

     //if it has numbers and characters, increase strength value 
     if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) strength += 2 

     //if it has one special character, increase strength value 
     if (password.match(/([!,%,&,@,#,$,^,*,?,_,~])/)) strength += 3 

     //if it has two special characters, increase strength value 
     if (password.match(/(.*[!,%,&,@,#,$,^,*,?,_,~].*[!,%,&,@,#,$,^,*,?,_,~])/)) strength +=5 

     //now we ha 
    if(strength<=2) 
    { 
     $scope.final="Poor"; 
    } 
    else 
     if(strength>2 && strength<=5) 
     { 
      $scope.final="Weak"; 
     } 
    else 
    if(strength>5 && strength<=9) 
    { 
     $scope.final="Good"; 
    } 
    if(strength>9) 
    { 
     $scope.final="Strong"; 
    } 

    } 

    } 
}); 
</script> 
+1

あなたが理解する権利を得た場合は、単に明確にする - 入力が触れた(何も入力された)、その後、クリアされた場合でも、**それはまだ$ dirty' 'と考えられています**。言い換えれば、**入力した入力は、明示的に設定しない限り、入力を元に戻すことはできません。(テキスト値をクリアするだけで元の状態にはならない)** –

+1

'$ dirty'と' $ pristine'は逆です。そのうちの1つをチェックするだけです。 – zeroflagL

+0

だから私は$ dirtyや!$ pristineを使うことができました。もし 'if($ scope.myfrm.textchecker。$ dirty &&!$ scope.myfrm.textchecker。$ invalid)'を使うと良い解決法になるのでしょうか? – rawatdeepesh

答えて

2

使用するために、コントローラのフォームの$dirty & $pristineには、$scopeでアクセスする必要があります。

if ($scope.myfrm.textchecker.$dirty && !$scope.myfrm.textchecker.$pristine) { 

var app = angular.module("myApp", []); 
 
app.controller("outerCtrl", function($scope) { 
 

 

 

 
    $scope.processed = function(password) { 
 
    if ($scope.myfrm.textchecker.$dirty && !$scope.myfrm.textchecker.$pristine) { 
 
     console.log('!myfrm.textchecker.$dirty + !myfrm.textchecker.$pristine'); 
 

 
     console.log(!myfrm.textchecker.$dirty + !myfrm.textchecker.$pristine); 
 
     // var password=$scope.ngdata; 
 
     var strength = 0; 
 
     //console.log(password); 
 
     // alert($scope.ngdata); 
 
     // if (password.length > 7) strength += 1 
 

 
     //if password contains both lower and uppercase characters, increase strength value 
 
     if (password.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/)) strength += 1 
 

 
     //if it has numbers and characters, increase strength value 
 
     if (password.match(/([a-zA-Z])/) && password.match(/([0-9])/)) strength += 2 
 

 
     //if it has one special character, increase strength value 
 
     if (password.match(/([!,%,&,@,#,$,^,*,?,_,~])/)) strength += 3 
 

 
     //if it has two special characters, increase strength value 
 
     if (password.match(/(.*[!,%,&,@,#,$,^,*,?,_,~].*[!,%,&,@,#,$,^,*,?,_,~])/)) strength += 5 
 

 
     //now we ha 
 
     if (strength <= 2) { 
 
     $scope.final = "Poor"; 
 
     } else 
 
     if (strength > 2 && strength <= 5) { 
 
     $scope.final = "Weak"; 
 
     } else 
 
     if (strength > 5 && strength <= 9) { 
 
     $scope.final = "Good"; 
 
     } 
 
     if (strength > 9) { 
 
     $scope.final = "Strong"; 
 
     } 
 

 
    } 
 

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

 
<body ng-app="myApp"> 
 
    <div ng-controller="outerCtrl"> 
 
    <form name="myfrm"> 
 
     <input type="password" name="textchecker" ng-change="processed(ngdata)" ng-model="ngdata" required/> 
 

 
     <input type="submit" name="submit" value="submit"> 
 
    </form> 
 
    <span style="color:red" ng-show="myfrm.textchecker.$error.required">please enter the required text</span> 
 
    {{ngdata}} 
 
    <p> 
 
     {{final}} 
 

 
    </p> 
 

 
    <p> 
 
     $dirty : {{ myfrm.textchecker.$dirty }} $invalid : {{myfrm.textchecker.$invalid }} $pristine : {{myfrm.textchecker.$pristine }} 
 
    </p> 
 

 
    </div> 
 
</body>

+0

すてきなキャッチ、ありがとう。正規表現を使用せずにng-validationを使用しないでパスワード強度を見つける方法は他にもあります。ありがとうございました – rawatdeepesh

+1

あなたのアプローチは柔軟性とメンテナンスの良いチャンスを持っているかもしれないので、私はそれに固執するでしょう:)それがあなたを助けたら答えを受け入れる –

関連する問題