0

I have to format a date in an input textbox in javascript or angular, and I am facing an issue in my code.

HTML:

<input  type="text" class="form-control" name="dateOfBirth" ng-model="dob" placeholder="Birthday(MM/DD/YYYY)" ng-required="true" ng-change="change()" maxlength="10" ng-maxlength="10" dob-format>

JS:

 vm.change = function (e) {
    if (vm.dob) {
        var dobLen = vm.dob.length;
        if (dobLen === 2 || dobLen === 5)
            vm.dob = vm.dob + "/";
    }
};

If I type "6"(month) instead of "06", it must automatically change to "06", here I am facing an issue where the forward slash will come when I type two letters and the backspace is not working. So anyone please help me.

Any help would be appreciated.

5
  • what is vm here? Commented May 15, 2017 at 7:21
  • is that scope ? Commented May 15, 2017 at 7:21
  • @lalithkumar yes it is scope variable like var vm=this Commented May 15, 2017 at 7:22
  • You could probably make the date check on blur (instead of doing it in change). This way you could use Date.parse on the whole string and then use angular filters to format the date Commented May 15, 2017 at 7:24
  • @Phugo can you please explain,how to do using ng-blur? Commented May 15, 2017 at 7:25

4 Answers 4

1

Change accordingly in change event.

Regex_replace('0' + Replace([Starting Date], '/', '/0'), '0*(\d\d)/0*(\d\d)/0*(\d\d)', '20$3-$1-$2')
Sign up to request clarification or add additional context in comments.

2 Comments

what is this?can you just explain?
@AWE sorry I didn't understand.
0
public static getDateForm_german(date: Date): string {
    let filter = {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit'
    };
    return date.toLocaleString('de-DE', filter);  
    // returns like 30.02.1999 for 1999-02-30
}

i know it is not the format you are looking for, and it is typescript. but i think you are looking for something like this.

Comments

0

Try like below:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
          app.controller('appCtrl', ['$scope', function($scope){
              vm=$scope;
               vm.change = function (e) {
    if (vm.dob) {
        var dobLen = vm.dob.length;
        if (dobLen === 2 || dobLen === 5)
            vm.dob = vm.dob + "/";
    }
};
          }]);
</script>

HTML:

<div ng-app="myApp" ng-controller="appCtrl">
        <input  type="text" class="form-control" name="dateOfBirth" ng-model="dob" placeholder="Birthday(MM/DD/YYYY)" ng-required="true" ng-change="change()" maxlength="10" ng-maxlength="10" dob-format>
     </div> 

This is working like charm for me.

1 Comment

This is the remember to accept the answer if appropriate @pbsbr
0

A mixture of the pattern attribute and some onchange formatting can fix this:

var inp = document.body.appendChild(document.createElement("input"));
inp.pattern = "\\d\\d/\\d\\d/\\d\\d";
inp.placeholder = "YY/MM/DD";
inp.required = true;

function formatDate(evt) {
  var val = inp
    .value
    .toString().split("/")
    .map(function(a) {
      if (a.length == 1) {
        return "0" + a;
      }
      return a;
    })
    .join("/");
  if (/\d\d\/\d\d\/\d\d/.test(val)) {
    inp.value = val;
  }
}
inp.onchange = formatDate;

1 Comment

This does nothing at all for formatting and or validation.

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.