Expand my Community achievements bar.

SOLVED

Check for a valid date in a text field

Avatar

Former Community Member

Hello everybody!

I have a challenge that to me seems a bit tricky. I have a text field that appears as a comb of 11 characters. The user will enter digits in the format 999999-9999. Now I would like to check if the firs 6 digits are a valid date in the format ddmmyy. (the last 4 digits are random)

How can I do that?

Kirstine

1 Accepted Solution

Avatar

Correct answer by
Former Community Member

This needs to be tested THOROUGHLY. My testing was very limited. Given a two digit year the validation checks for two digits (\d\d) only.

// form1.page1.subform1.tf1::exit - (JavaScript, client)

if (!(this.isNull)) {

  var regEx = /^(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])\d\d$/;

  var str = this.rawValue;

  str = str.substring(0,6);

  if (regEx.test(str)) {

    xfa.host.messageBox("Valid mmddyy");

  }

  else {

    xfa.host.messageBox("Invalid mmddyy");

  }

}

Steve

View solution in original post

3 Replies

Avatar

Level 10

Validate them using Regular Expression.

Nith

Avatar

Correct answer by
Former Community Member

This needs to be tested THOROUGHLY. My testing was very limited. Given a two digit year the validation checks for two digits (\d\d) only.

// form1.page1.subform1.tf1::exit - (JavaScript, client)

if (!(this.isNull)) {

  var regEx = /^(0[1-9]|[12][0-9]|3[01])(0[1-9]|1[012])\d\d$/;

  var str = this.rawValue;

  str = str.substring(0,6);

  if (regEx.test(str)) {

    xfa.host.messageBox("Valid mmddyy");

  }

  else {

    xfa.host.messageBox("Invalid mmddyy");

  }

}

Steve

Avatar

Former Community Member

Hi Steve

Your script seams to do the job. Thanks a lot.

And to Nith,

Your point is all right. I just need more help than that as I’m a total newbie when it comes to scripting (any scripts at all) and I’m still not able to build my own scripts from scratch. It will come some day, I hope.

Regards,

Kirstine

The following has evaluated to null or missing: ==> liqladmin("SELECT id, value FROM metrics WHERE id = 'net_accepted_solutions' and user.id = '${acceptedAnswer.author.id}'").data.items [in template "analytics-container" at line 83, column 41] ---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression, use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)?? ---- ---- FTL stack trace ("~" means nesting-related): - Failed at: #assign answerAuthorNetSolutions = li... [in template "analytics-container" at line 83, column 5] ----