-2

I have this script in js:

CODE HTML:

<input class="required-input" type="text" name="date_of_birth" id="date_of_birth" placeholder="MM/DD/YYYY">

CODE JS:

$('.required-input').each(function() {
             $(this).on('input keyup keypress blur change', function() {
        const dob=  /^([0]?[1-9]|[1|2][0-9]|[3][0|1])[./-]([0]?[1-9]|[1][0-2])[./-]([0-9]{4}|[0-9]{2})$/;
        var regex;

        if ($(this).attr("id") == "date_of_birth") 
            {
                 regex = dob;
            }

        //SOME CODE JS:

         });
     });

I want these things:

1.To be the next format 23/09/1992

2.You can not write more characters than necessary.

3.You can not write letters, only numbers

How to change this variable (dob) to be good?

Thanks in advance!

Marius
  • 1,181
  • 3
  • 15
  • 23
  • Why not use jquery date picker instead? – User2012384 Jan 07 '16 at 08:55
  • depending on your browsers that you have to support (based on the `const`, I am guessing that only modern) you can use the input of type `date`. That will take care of most of what you want. It has build in validation. Built right into the browser. This way you don't need regex http://www.w3schools.com/html/tryit.asp?filename=tryhtml_input_date – frosty Jan 07 '16 at 08:55
  • Your `dob` regex is for DD/MM/YYYY date pattern, but then you have `placeholder="MM/DD/YYYY"`. If you plan to only support `/` delimiters remove others from the `[./-]` class. And `/` must be escaped in a literal notation regex. – Wiktor Stribiżew Jan 07 '16 at 08:56
  • Yes, but for example if a user writes: 23091992...I want to transform this text in 23/09/1992 auto – Marius Jan 07 '16 at 08:58
  • Have a look at [this demo](http://jsfiddle.net/mpdkn4je/). I think it is all you need if you use JQuery. – Wiktor Stribiżew Jan 07 '16 at 09:51

1 Answers1

0
^(19|20)\d\d(/)(0[1-9]|1[012])\2(0[1-9]|[12][0-9]|3[01])$

See here for more.


You can also see jQuery Mask


Consider also using native HTML5 validation attribute:

squaleLis
  • 6,116
  • 2
  • 22
  • 30