title: Time validation in angular js
body: <p>I have a field which takes inputs of time something like 12:00 / 24:00
  I want used to be able tot fill only digits and : in the field and on change validate it with proper time. I want to do it in angular way. How do i do it. </p>
  
  <p>Here is the code i have written. This can be done in 1 directive also. This code works, But not better code.</p>
  
  <pre><code>&lt;input  type="text" ng-model-options="{allowInvalid: true}" ng-blur="validateHhMm(this)" ng-model='time' time-only/&gt;
  
  
  angular.module('abc').directive('timeOnly', function(){
      return {
          require: 'ngModel',
          link: function(scope, element, attrs, modelCtrl) {
  
              modelCtrl.$parsers.push(function (inputValue) {
                  var transformedInput = inputValue ? inputValue.replace(/[^\d:]/g,'') : null;
  
                  if (transformedInput!=inputValue) {
                      modelCtrl.$setViewValue(transformedInput);
                      modelCtrl.$render();
                  }
  
                  return transformedInput;
              });
          }
      };
  });
  
  
  $scope.validateHhMm =  function (inputField)
      {
          var errorMsg = "";
          var regs = '';
          // regular expression to match required time format
          var re = /^(\d{1,2}):(\d{2})?$/;
          var inputvalue = $scope.time;
          if(inputvalue != '')
          {
              if(regs = inputvalue.match(re))
              {
              if(regs[4])
              {
                  // 12-hour time format with am/pm
                  if(regs[1] &lt; 1 || regs[1] &gt; 12)
                  {
                      errorMsg = "Invalid value for hours: " + regs[1];
                  }
              }
              else
              {
                  if(regs[1] &gt; 23)
                  {
                      errorMsg = "Invalid value for hours: " + regs[1];
                  }
              }
              if(!errorMsg &amp;&amp; regs[2] &gt; 59)
              {
                  errorMsg = "Invalid value for minutes: " + regs[2];
              }
              }
              else
              {
                  errorMsg = "Invalid time format: " + inputvalue;
              }
          }
  
          if(errorMsg != "")
          {
              //console.log(errorMsg);
              //field.focus();
              $scope.time = '';
              return false;
          }
          return true;
      };
  </code></pre>