Can we give date in "single line text" field in certain format(MM-DD-YYYY) and restrict other format

JuhiV
Tera Contributor

Hello Team,

 

I have a requirement to have a "single line text field" in catalog form for date in which we have to enter Date in certain format(MM-DD-YYYY). There has to be an error message if the users are entering the date in different format. Please help me in achieving this.

 

2 REPLIES 2

Simon Christens
Mega Sage

Hi @JuhiV 


Its not recommended doing this though in case you really want it.

In my example i made a variable named "date" - single line text variable

Then make an "onChange" Client script on that variable

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {

        g_form.hideFieldMsg('date');
        return;
    }

    var regex = /((0[13578]|1[02])[-]31[-](18|19|20)[0-9]{2})|((01|0[3-9]|1[1-2])[-](29|30)[-](18|19|20)[0-9]{2})|((0[1-9]|1[0-2])[-](0[1-9]|1[0-9]|2[0-8])[-](18|19|20)[0-9]{2})|((02)[-]29[-](((18|19|20)(04|08|[2468][048]|[13579][26]))|2000))/g;

    if (!regex.test(newValue)) {

        g_form.showFieldMsg('date', 'error in format', 'error');
    }
}

 Now, Its hard to maintain though the regex will check for month (1-12) - day (1-31) - year (19xx and / or 20xx)
The reason why its so complex is that February for ex doesnt have 30 or 31 days and every other month except july/august and december/january that also needs to be checked.

The same goes for leap year (every 4 year theres an extra day in february)

Ankur Bawiskar
Tera Patron

@JuhiV 

I will suggest to use Data variable instead of complex scripting and validation

If my response helped please mark it correct and close the thread so that it benefits future readers.