【问题标题】:Allowing alphanumeric and dot in scope允许范围内的字母数字和点
【发布时间】:2013-09-30 05:56:16
【问题描述】:

如何在范围内只允许字母数字字符和点

<input type="text" ng-model="test" />

脚本

$scope.myFn = function(){
if($scope.test != ''){
alert("Please use only Alphanumeric characters or dots")
}

}

【问题讨论】:

    标签: javascript regex angularjs


    【解决方案1】:

    更好的方法是在 html 输入元素上定义一个ng-pattern。这将不允许在模型上设置不正确的值。我没有测试过正则表达式模式。

    <input type="text" ng-model="test" ng-pattern="/[a-zA-Z0-9\.]*/"/>
    

    【讨论】:

    • 正则表达式需要锚定:ng-pattern="/^[a-zA-Z0-9\.]*$/"
    【解决方案2】:
        if (/[^a-zA-Z0-9\.]/.test($scope.test)) {
            alert("Please use only Alphanumeric characters or dots")
        }
    

    【讨论】:

      猜你喜欢
      • 2014-04-11
      • 1970-01-01
      • 2021-07-23
      • 2019-12-09
      • 2015-06-08
      • 2018-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多