Javascript date mask

Hi E's,
I have a date mask, but not work perfect.
The code is this one:
<input id="lancamento" value="<? echo $linha[data2]; ?>" type="text" name="date" placeholder="yyyy-mm-dd" onkeyup="var v = this.value; if (v.match(/^\d{4}$/) !== null){this.value = v + '-';} else if (v.match(/^\d{4}\/\d{2}$/) !== null) {this.value = v + '-';}" maxlength="10">

Open in new window

The mask as been create for format data: yyyy-mm-dd.
The problem is, the last "-" not appear, and when I write the date occurs something like this: 2018-0422, when the correct is 2018-04-22!

What is wrong in my code?

The best regards, JC
Pedro ChagasWebmasterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Leonidas DosasCommented:
What is the echo value in the input?
Pedro ChagasWebmasterAuthor Commented:
Hi @Leonidas,
You can see the echo in attach image.
Can you please help me?

Leonidas DosasCommented:
Check this function
function a(arg){ 
  var y,x,z;  
  return x;

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Pedro ChagasWebmasterAuthor Commented:
Hello @Dosas,
Can you please show me how to relate your solution to the input?

Thank you.
David S.Commented:
That second condition will never match, since it's checking for a different separator.

Why are you using inline events anyway?

You can simplify the code by using one regular expression to check for both conditions. Also, when you simply want to know if the expression matches or not, it's more efficient to use RegExp.test() instead of String.match().
var v = this.value; if (/^\d{4}(-\d{2})?$/.test(v)){this.value = v + '-';}

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
Advice - don't put your code inline. Create a function for it and call that - it will be much easier to debug.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.