angularjs输入框不允许输入空格

来源:互联网 发布:union软件下载 编辑:程序博客网 时间:2024/06/06 09:55

html部分代码:

<!-- 修改密码 --><figure class="pwd_wrap main_wrap" ng-show="change_pwd_window">    <figcaption>        <strong>修改密码</strong>        <i class="fa fa-times" aria-hidden="true" ng-click="change_pwd_window=false"></i>    </figcaption>    <div>        <form class="form-horizontal change_pwd_wrap" name="pwd_frm" method="post" novalidate autocomplete="off">            <div class="form-body">                <div class="pwd_item">                    <label class="title">账号:</label>                    <div class="value">                        <input type="text" class="form-control" readonly="readonly" ng-model="user_name">                    </div>                </div>                <div class="pwd_item">                    <label class="title">原密码:</label>                    <div class="value">                        <input type="password" ng-keyup="NoSapceInput($event,'old_pwd')" class="form-control" ng-model="old_pwd" />                    </div>                </div>                <div class="pwd_item">                    <label class="title">新密码:</label>                    <div class="value">                        <input type="password"  ng-keyup="NoSapceInput($event,'new_pwd')" name="new_pwd" class="form-control" ng-model="new_pwd" ng-minlength="6" ng-maxlength="18" required/>                    </div>                </div>                <div class="field-message valid_pwd" ng-messages="pwd_frm.new_pwd.$error" ng-if='pwd_frm.new_pwd.$touched&&pwd_frm.new_pwd.$dirty'                    ng-cloak>                    <div ng-message="required">密码是必须的</div>                    <div ng-message="minlength">密码长度必须不少于6</div>                    <div ng-message="maxlength">密码长度必须不大于18</div>                </div>                <div class="pwd_item">                    <label class="title">确认密码:</label>                    <div class="value">                        <input type="password" class="form-control" ng-keyup="NoSapceInput($event,'confirm_pwd')" ng-model="confirm_pwd">                    </div>                </div>                <div class="pwd_item">                    <button ng-click="save_pwd()" ng-disabled="pwd_frm.$invalid">确认</button>                    <button data-dismiss="modal" ng-click="change_pwd_window=false">取消</button>                </div>            </div>        </form>    </div></figure>

js部分代码:

    //禁止输入空格    function Trim(str, is_global) {        var result;        result = str.replace(/(^\s+)|(\s+$)/g, "");        if (is_global.toLowerCase() == "g") {            result = result.replace(/\s/g, "");        }        return result;    }    $scope.NoSapceInput = function (e,input_name) {        var curValue = e.target.value;        $scope[input_name] = curValue;        $scope[input_name] = Trim(curValue, "g");         console.log($scope[input_name]);        var keynum;        if (window.event) // IE         {            keynum = e.keyCode        }        else if (e.which) // Netscape/Firefox/Opera         {            keynum = e.which        }        if (keynum == 32) {            return false;        }        return true;    }