Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Using regular expressions for validating time fields

Avatar

Level 2

Similar to my problem with converting a big chunk of validation into smaller chunks of functions I am trying to use Regular Expressions to handle the validation of many, many time fields in a flexible working time sheet.

I have a set of FormCalc scripts to calculate the various values for days, hours and the gain/loss of hours over a four week period. For these scripts to work the time format must be in HH:MM.

Accessibility guidelines nix any use of message box pop ups so I wanted to get around this by having a hidden/visible field with warning text but can't get it to work.

So far I have:

var r = new RegExp(); // Create a new Regular Expression Object
r.compile ("^[00-99]:\\] + [00-59]");

var result = r.test(this.rawValue);

if (result == true){
true;
form1.flow.page.parent.part2.part2body.errorMessage.presence = "visible";
}
else (result == false){
false;
form1.flow.page.parent.part2.part2body.errorMessage.presence = "hidden";
}

Any help would be appreciated!

1 Reply

Avatar

Former Community Member

Date and time fields are tricky because you have to consider the formattedValue versus the rawValue. If I am going to use regular expressions to do validation I find it easier to make them text fields and ignore the time patterns (formattedValue). Something like this works (as far as my very brief testing goes) for 24 hour time where time format is HH:MM.

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


var error = false;

form1.page1.subform1.errorMsg.rawValue = "";


if (!(this.isNull)) {

  var time_ = this.rawValue;

  if (time_.length != 5) {

    error = true;

  }

  else {

    var regExp = /^([01]?[0-9]|2[0-3]):[0-5][0-9]$/;

    if (!(regExp.test(time_))) {

      error = true;

    }

  }

}


if (error == true) {

  form1.page1.subform1.errorMsg.rawValue = "The time must be in the format HH:MM where HH is 00-23 and MM is 00-59.";

  form1.page1.subform1.errorMsg.presence = "visible";

}

Steve