javascript ng 模式,它允许字母数字但没有空格用于 angular js 中的输入标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26260855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ng pattern which allows alphanumeric but without spaces for input tag in angular js
提问by swapna
am using the patterns like "/[a-zA-Z0-9][^\s]/" and "/\s/" but no one is working. can you give any solution for this "ng pattern for input tag and which allows oly alphanumeric but it does not allow spaces in the input field ".
我正在使用诸如“/[a-zA-Z0-9][^\s]/”和“/\s/”之类的模式,但没有人在工作。您能否为此“输入标签的ng模式提供任何解决方案,它允许使用oly字母数字,但不允许在输入字段中使用空格”。
回答by Max Sassen
Try the following pattern: /^\s*\w*\s*$/
尝试以下模式: /^\s*\w*\s*$/
<script>
angular.module('textInputExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.text = 'guest';
$scope.word = /^\s*\w*\s*$/;
}]);
</script>
<form name="myForm" ng-controller="ExampleController">
Single word: <input type="text" name="input" ng-model="text" ng-maxlength="5"
ng-pattern="word" required ng-trim="">
回答by sylwester
Please see here http://jsbin.com/ruqet/1/edit
请看这里http://jsbin.com/ruqet/1/edit
<form name="myform">
<input type="text" ng-model="val" ng-pattern="/^[a-zA-Z0-9]*$/" name="abc"/>
<br/>
<span ng-show="myform.abc.$error.pattern">Please use numbers letters only</span>
</form>
回答by Raj
<form name="testFrm">
<input type="text" ng-model="val" ng-pattern="/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/" name="value"/>
<span ng-show="testFrm.value.$error.pattern">Please use numbers and letters only</span>
</form>