获取从输入类型密码到范围的尾随空格? [英] Get trailing white space from input type password to scope?

查看:15
本文介绍了获取从输入类型密码到范围的尾随空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个输入密码字段.我希望能够警告用户他们的密码中不能有任何空格.我发现的问题是我无法将尾随的空白传递给范围以检测它并警告用户他们不能这样做.

I have an input password field. I want to be able to warn the user that they cannot have any white space in there password. The trouble I'm finding is that I cant pass the trailing white space to the scope in order to detect it and warn the user that they cannot do this.

查看我的 plunkr 示例:LINK

See my plunkr example: LINK

如果您在输入字段中输入,范围将返回密码的字符数,如果您在密码末尾添加空格,范围将不会报告正确的字符串长度,因为它显然会修剪任何尾随空格这意味着我无法确定用户是否正在输入任何空格.因此,当用户添加尾随空格时,当范围仅报告字符长度而没有任何尾随空格时,输入密码字段将显示已添加额外字符.

If you type in the input field the scope will return how many characters the password is and if you add white space at the end of the password the scope will not report the correct string length because it is obviously trimming any trailing white space which means I have no way to identify if the user is entering any spaces or not. So as the user adds trailing spaces the input password field will show that an extra character has been added when the scope is only reporting the length of characters without any trailing spaces.

推荐答案

这里的固定 plunkr 解决方案是升级到 angular 1.1.1 以利用 ng-trim 指令,该指令允许您关闭修剪:http://plnkr.co/edit/FLCQY2zuRV1ZMy6WCbs8?p=preview

Here's the fixed plunkr solution is upgrade to 1.1.1 of angular to take advantage of ng-trim directive which allows you to turn off the trimming: http://plnkr.co/edit/FLCQY2zuRV1ZMy6WCbs8?p=preview

升级到 Angular 1.1.1 或更高版本(已测试,可能适用于某些较低版本)将此指令添加到您的元素中,其中您有不想修剪的 ng-model.

Upgrade to Angular 1.1.1 or higher (tested, might work in some lower versions) add this directive to your element where you have the ng-model you don't want trimmed.

ng-trim="false"

这里是完整的细节:

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="angular.js@1.1.x" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.1/angular.min.js" data-semver="1.1.1"></script>
    <script src="angular_ui.js"></script>
    <script src="app.js"></script>
  </head>

  <body ng-controller="MainCtrl">
    <form>
      Pass length is {{pass.length}}<br>
      <input type="password" data-ng-model="pass" data-ng-trim="false">
    </form>
  </body>

</html>

还有 JS

var app = angular.module('plunker', ['ui.event']);

app.controller('MainCtrl', function($scope) {
  $scope.name = 'World';
});

这篇关于获取从输入类型密码到范围的尾随空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆