angular js ng-keyup不能用于密码强度检查

时间:2016-03-01 06:26:40

标签: javascript angularjs

我是角度编程的新手。

所以我被赋予了一个任务来创建一个角度的应用程序,以根据输入字符串长度检查密码的强度。

以下是我的代码,请检查一下。

public class MySurfaceView extends SurfaceView {
private static final String TAG = "FreeHandDrawing";
public static Canvas mCanvas;
SurfaceHolder holder;
private static Path path;
private Paint paint;
private ArrayList<Path> pathArrayList = new ArrayList<>();
private boolean freeHandMode;

public MySurfaceView(Context context, AttributeSet attrs) {
    super(context, attrs);
    freeHandMode = false;
    path = new Path();
    holder = getHolder();
    holder.setFormat(PixelFormat.TRANSPARENT);
    setDrawingCacheEnabled(true);

    this.setZOrderOnTop(true);

    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(0xFF22FF11);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setStrokeWidth(8);

}


@Override
public boolean onTouchEvent(MotionEvent event) {

    if(freeHandMode) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            Log.d("Action", "Placed");
            path.moveTo(event.getX(), event.getY());

        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {

            Log.d("Action", "Moved");
            path.lineTo(event.getX(), event.getY());
            pathArrayList.add(path);

        }

        mCanvas = holder.lockCanvas();
        if (mCanvas != null) {
            if (pathArrayList.size() > 0) {

                mCanvas.drawPath(pathArrayList.get(pathArrayList.size() - 1), paint);

            }
            holder.unlockCanvasAndPost(mCanvas);

        } else {
            Log.d(TAG, "Canvas is NULL");
        }
    }
    invalidate();
    return true;
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Log.d(TAG, "On draw called");

}

public void eraseDrawing() {
    pathArrayList.clear();
    invalidate();
}

public void drawEnableDisable(boolean mode) {
    freeHandMode = mode;
  }
}                                                  

我面临的问题是应用程序无法运行。 当字符串长度低于5或低于8时,根据条件,div颜色应随其验证而变为红色。但它没那样工作..请帮忙。

请大家详细解答,因为我是这个脚本的初学者。 谢谢。

3 个答案:

答案 0 :(得分:1)

我认为你的问题是将nd-model定义为ng-model。 我把你的样品放在这里并且工作正常。

使用ng-change insted ng-keypress。现在它正常工作。

var ans= angular.module("myapp",[]);
        ans.controller("pswdstr",function($scope){
            $scope.show=function(){
                var pswdlen= $scope.pswd.length;
                if (pswdlen==0) 
                {
                    $scope.color="";
                    $scope.validation="Enter password";
                }
                else if (pswdlen<=5) 
                {
                    $scope.color="red";
                    $scope.validation="password is weak";
                }
                else if (pswdlen>5 && pswdlen<=8) 
                {
                    $scope.color="orange";
                    $scope.validation="password is ok";
                }
                else if (pswdlen>8) 
                {
                    $scope.color="green";
                    $scope.validation="password is strong";
                };

            };

        });
.red{
        background-color: red;
    }
    .orange{
        background-color: orange;
    }
    .green{
        background-color: green;
    }
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp" ng-controller="pswdstr">
<label for="pswd">Enter password</label>
<input type="password" name="pswd" id="pswd" ng-model="pswd" ng-change="show()">
<div class='{{ color }}' ng-if="pswd">
{{ validation }}
</div>
</div>

答案 1 :(得分:1)

您可以在~

中使用ng-change代替ng-keypress

请参阅here工作代码

答案 2 :(得分:0)

使用keydown的instydown和

的instread
 $scope.pswd.length 

使用

 $scope.pswd.length+1 

并在支票中提醒您是否正常工作