Search code examples
angularjsregexng-pattern

Should not allow the space in password field?


Need to show error message whenever i enter space in password input field using ng-pattern.


Solution

  • Use of regEx - /^\S*$/ does not allow space anywhere in the string.

    <form name="myForm">
       <input type='password' name="passInpt" ng-model='pass' data-ng-pattern="/^\S*$/">
       <div data-ng-show="myForm.passInpt.$error.pattern" >White Space not allowed</div>
    </form>
    

    DEMO:

    var app = angular.module("app", []);
    app.controller("ctrl", function($scope) {});
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
    
    <body ng-app="app" ng-controller="ctrl">
      <form name="myForm">
        <input type='password' name="passInpt" ng-model='pass' data-ng-pattern="/^\S*$/">
        <div data-ng-show="myForm.passInpt.$error.pattern">White Space not allowed</div>
      </form>
    </body>