0

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 3

3

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="">

Plunkr link

Sign up to request clarification or add additional context in comments.

Comments

2

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

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.
@swapna you can add required please see updated jsbin jsbin.com/ruqet/2/edit
This pattern works if you also add ng-trim="false" to the input. See this duplicate question/answer here: stackoverflow.com/a/19424059/7032554
0
<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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.