python ruby-programming-language Smalltalk Vue.js Knockoutjs TypeScript JavaScript, OOPs JavaScript Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 8,7,6,5,4 and 2 Interview Questions and Answers Angular 4 Angular 2 AngularJs 1.x NodeJs Perl Programming R Programming RequireJs Rust Programming Backbonejs closure programming language go-programming-language kotlin-programming-language
angularjs required validation using ng-pattern

angularjs required validation using ng-pattern

This code sample are used to validate a required field with single word which are given below.

Table of Contents.
  1. input type is equal to text [type="text"].
  2. ng-pattern with single word.
  3. Required element tag.
  4. ng-model with two way binding.
Example code

<!doctype html>
<html>
<head>
  <title> angularjs requiredvalidation </title>
  <script src="http://code.angularjs.org/1.2.9/angular.min.js"></script>
  <style>
    .error {
      color: red;
    }
  </style>
  <script>
      var app = angular.module('myApp', []);

      app.controller('mainCtrl', function ($scope) {
          $scope.username = '';
          $scope.singleword = /^\s*\w*\s*$/;
      });
  </script>
</head>

<body ng-app="myApp">
  <ng-form name="myForm" ng-controller="mainCtrl">
    <div>UserName :</div>
    <div>
      <input type="text" name="name" ng-model="username" ng-pattern="singleword" required>
      <span class="error" ng-show="myForm.name.$error.required">Required!</span>
      <span class="error" ng-show="myForm.name.$error.pattern">Only single word.</span>
    </div>
  </ng-form>
</body>

</html>

The result look like below image.


ANIL SINGH

Hi there! Welcome to my blog. My name is Anil Singh. I'm a author of this blog. For more detail about me, kindly refer to this link..
My Blogs - http://code-sample.com and http://code-sample.xyz
My Books - Interview Questions and Answers Books- Get Your Book in 15+ Digital Stores Worldwide..

You Might Also Like
www.code-sample.com/. Powered by Blogger.
ASK Questions