【问题标题】:restrict special characters into input box将特殊字符限制在输入框中
【发布时间】:2019-08-18 19:10:12
【问题描述】:

我是 Angular 的新手。需要使用 angularjs 将特殊字符限制在输入框中

HTML 代码:

<input type="text" class="form-control" id="cntry" ng-model="address.countryCode">

只允许字母或数字

【问题讨论】:

标签: html angularjs typescript


【解决方案1】:

您可以通过添加模式来尝试此解决方案:

<input type="text" class="form-control" id="cntry"
       ng-model="address.countryCode" pattern="^[a-zA-Z0-9]*$}">

【讨论】:

    【解决方案2】:

    在你的控制器中定义一个正则表达式

    $scope.regex = /^[^`~!@#$%\^&*()_+={}|[\]\\:';"<>?,./1-9]*$/;
    

    并且在您的 html 中使用 ng-pattern 指令并将上述正则表达式作为模式传递。

    <input type="text" class="form-control" id="cntry"
           ng-model="address.countryCode" ng-pattern="regex">
    

    欲了解更多信息,请访问Restrict Special Characters in HTML & AngularJs

    【讨论】:

      猜你喜欢
      • 2018-10-30
      • 1970-01-01
      • 2012-04-24
      • 2018-12-27
      • 2018-10-31
      • 2018-11-07
      • 2021-07-03
      • 2018-10-25
      • 1970-01-01
      相关资源
      最近更新 更多