将特殊字符限制在输入框中
Posted
技术标签:
【中文标题】将特殊字符限制在输入框中【英文标题】: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 问题,那么你试试这个它可能有助于只接受大写字母或小写字母。只需添加此模式="[A-Za-z]3" --- 检查w3schools.com/tags/tryit.asp?filename=tryhtml5_input_pattern & w3schools.com/tags/att_input_pattern.asp 【参考方案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
【讨论】:
以上是关于将特殊字符限制在输入框中的主要内容,如果未能解决你的问题,请参考以下文章
JavaScript 中怎样判断文本框只能输出英文字母、汉字和数字,不能输入特殊字符!