Need help with regex to allow users to enter time zone abbrv (3-4 characters)

lando321
Tera Contributor

Hello everyone,

I need some assistance with updating regex on a field for my catalog item, this field functions as a time field and i want users to be able to enter time zones after the ex. 12:30 PM EST or 11:20 PM ACDT, my regex below is allowing for anything to be entered into the field.

 

 

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

     g_form.hideFieldMsg('time');


   var str = newValue; //the newValue comes from the onChange function's parameters



   var pattern = new RegExp(/\b(0?\d|1[0-2]):[0-5]\d(\s)?([AaPp][Mm]$)/([A-z])).test(str); //checks for 1:23:45 or 01:23:45


   if (!pattern) {


   g_form.showFieldMsg('time','Please use the right format.','error');


   return pattern;


   } else {


   g_form.hideAllFieldMsgs();


   }


}
   

 

 

1 ACCEPTED SOLUTION

DanielCordick
Mega Patron
Mega Patron

Try this pattern ^\d{1,2}:\d{2}(?:am|pm)?\s+[A-Z]{2,4}$

 

it should only match inputs where a time is followed by a time zone (time zone in upper case)  eg 12:30pm EST or 10:30am PST

 

how are you determining if the time zone is correct? 

 

Mark Correct if this solves your issue and also mark 👍Helpful if you find my response helped in any way 

 

View solution in original post

4 REPLIES 4

DanielCordick
Mega Patron
Mega Patron

Try this pattern ^\d{1,2}:\d{2}(?:am|pm)?\s+[A-Z]{2,4}$

 

it should only match inputs where a time is followed by a time zone (time zone in upper case)  eg 12:30pm EST or 10:30am PST

 

how are you determining if the time zone is correct? 

 

Mark Correct if this solves your issue and also mark 👍Helpful if you find my response helped in any way 

 

@DanielCordick hmmm good question, i am not currently checking the time zone for accuracy.

Did you test out the pattern? Did it work?

I did test it out, it works, really appreciate you responding.