Regex datetime validation - Validation Script - Date/Time

georgechen
Kilo Guru

Hi folks,

I have a regex validation question for your advice this time.   In ServiceNow it has a neat trick with DateTime field.     When entering 03-02-2014 00:00:00 it rolls the calendar forward to March 2nd.   We've replaced the recommended getDateFromFormat() function with a regex statement so we can limit but keep this feature, however, the problem we got is people entering dates without numbers.  

Here is the custome validator

function validate(value) {

      if (!value) {

              return true;

      } else {

              var reg = /^([0-9]|[0-9][0-9])[\-]([0-9]|[0-9][0-9])[\-]2(\d{3})[ ](\d{1}|\d{2}|\d{3}):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])$/;

              //(getDateFromFormat(value,'dd-MM-yyyy HH:mm:ss') != 0);

              if(reg.test(value.trim())) {

                      return true;

              } else {

                      return new GwtMessage().getMessage("Invalid dateTime")                      

              }

      }

}

Is there a way to amend the validator that will accept a date only without number?

Thanks,

3 REPLIES 3

georgechen
Kilo Guru

//Original regex


//var reg = /^([1-9]|0[1-9]|[12][0-9]|3[01])[\-]([1-9]|0[1-9]|1[012])[\-]2(\d{3})[ ]([1-9]|0[0-9]|1[0-9]|2[0123]):(0[0-9]|[1-5][0-9]):(0[0-9]|[1-5][0-9])$/;


days = 1 to 9, 01 to 09, 10 to 19, 21 to 29, 30 to 31


months = 1 to 9, 01 to 09, 10 to 12


years = 2 + 3 digits (digits, not characters)


hours = 1 to 9, 01 to 09, 10 to 19, 20 to 23


minutes = 01 to 09, 10 to 59


seconds = 01 to 09, 10 to 59


Brian Dailey1
Kilo Sage

Hi George,



I'm not clear on what you mean by "accept a date only without number".   Please elaborate.




Thanks,


-Brian


Thanks Brain for your attention.     If a user enter only date like 2016-02-26 without entering 00:00:00, the regex rejects it.



I hope this helped.


Cheers,