Avatar of maqskywalker
maqskywalker
 asked on

jquery textbox date format check

I'm using jquery, bootstrap, jquery.validate in my asp.net MVC 5 application.
I have seeing various solutions but not sure a good stable solution.

In a textbox the value it contains on page load is a date in this format 11/05/2017

Is there a way to do the following. Wether it's with jquery or JavaScript or bootstrap or validate,

If the value contained in the textbox is anything besides a date display a tooltip that says "please enter a date in this format MM/DD/YYYY"

So if I click in the textbox and , if I delete one digit and the text looks like this 11/05/201  at that point the tooltip already appears. If I type the digit back in like this 11/05/2017 the tooltip disappears.

So the tooltip only appears when the textbox value is anything besides a date in this format MM/DD/YYYY
JavaScriptjQuery

Avatar of undefined
Last Comment
maqskywalker

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Leonidas Dosas

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
maqskywalker

ASKER
Thanks! Elegant solution!
maqskywalker

ASKER
Leonidas,

I noticed the tooltip displays as soon as i click in the textbox.

So i looked at this line trigger: "focus"

if i set the default value on the textbox to a good format like this which is 10 digits:   11/05/2017

what's the trigger so the tooltip launches when I click in the textbox and change the text from 10 digits to 9 digits like this: 11/05/201
Leonidas Dosas

With a default value  add this in script additionally with the previous:
$("#myText").focus(function() {
    if (this.value.length !==10 && parseDMY(this.value)!=='Invalid Date') {
        $(this).tooltip("show");
    } else {
        $(this).tooltip("hide");
    }
}).tooltip({
    placement: "right",
    trigger: "focus"
});

Open in new window

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
maqskywalker

ASKER
perfect!