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 ".
3 Answers
Try the following pattern: /^\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="">
Comments
Please see here 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>
3 Comments
swapna
But it does not display error message when we enter space into the text box. This is for alphanumeric [a-zA-Z0-9] working but the input field does not accept spaces.
sylwester
@swapna you can add required please see updated jsbin jsbin.com/ruqet/2/edit
Subjective Reality
This pattern works if you also add
ng-trim="false" to the input. See this duplicate question/answer here: stackoverflow.com/a/19424059/7032554