Dibish
Dibish

Reputation: 9293

Regular expression for valid string timezone check

I have requirement to validate timezone format. I am getting timezone in string format like the following way.

It should be like this way. I need to throw an error if anything else comes like +05:30

Valid timezone formats

 0, 1, 2, 3.5, 5.75 .. 12 
-1, -2, -4.5, -11 ... -12

Example timezone format

<option value="-12">(GMT -12:00)</option>
<option value="-11">(GMT -11:00) </option>
<option value="-10">(GMT -10:00)</option>
<option value="-9">(GMT -9:00) Alaska</option>
<option value="-8">(GMT -8:00)</option>
<option value="-7">(GMT -7:00)</option>
<option value="-6">(GMT -6:00)</option>
<option value="-5">(GMT -5:00)</option>
<option value="-4.5">(GMT -4:30)</option>
<option value="-4">(GMT -4:00)</option>
<option value="-3.5">(GMT -3:30) </option>
<option value="-3">(GMT -3:00)</option>
<option value="-2">(GMT -2:00)</option>
<option value="-1">(GMT -1:00 hour)</option>
<option value="0">(GMT)</option>
<option value="1">(GMT +1:00 hour)</option>
<option value="2">(GMT +2:00)</option>
<option value="3">(GMT +3:00)</option>
<option value="3.5">(GMT +3:30)</option>
<option value="4">(GMT +4:00)</option>
<option value="4.5">(GMT +4:30)</option>
<option value="5">(GMT +5:00) </option>
<option value="5.5">(GMT +5:30) </option>
<option value="5.75">(GMT +5:45)</option>
<option value="6">(GMT +6:00)</option>
<option value="6.5">(GMT +6:30)</option>
<option value="7">(GMT +7:00) </option>
<option value="8">(GMT +8:00) </option>
<option value="9">(GMT +9:00) </option>
<option value="9.5">(GMT +9:30) </option>
<option value="10">(GMT +10:00) </option>
<option value="11">(GMT +11:00) </option>
<option value="12">(GMT +12:00)</option>

Could you help me to solve this, i dont have much expertise in using regular expression to show what i had tried. Thanks

Upvotes: 1

Views: 3216

Answers (5)

Saifullah khan
Saifullah khan

Reputation: 778

This regex works for me:

  const timezoneOffsets = [
  '-11:00',
  '-10:00',
  '-09:00',
  '-08:00',
  '-07:00',
  '-06:00',
  '-05:00',
  '-04:00',
  '-03:00',
  '-02:00',
  '-01:00',
  '+00:00',
  '+01:00',
  '+02:00',
  '+03:30',
  '+04:30',
  '+05:45',
  '+06:30',
  '+07:00',
  '+08:00',
  '+09:30',
  '+10:00',
  '+11:00',
  '+12:00',
  '+13:00',
  '+14:00',
  '+200:00', // invalid
  '-200:00', // invalid
  '+200:001', // invalid
];

const regex = /^([+|-]{1})([0-1]{1}[0-9]{1}):([0-6]{1}[0-9]{1})$/;

for (const timezone of timezoneOffsets) {
  console.log(timezone, regex.test(timezone));
}

Output:

-11:00 true
-10:00 true
-09:00 true
-08:00 true
-07:00 true
-06:00 true
-05:00 true
-04:00 true
-03:00 true
-02:00 true
-01:00 true
+00:00 true
+01:00 true
+02:00 true
+03:30 true
+04:30 true
+05:45 true
+06:30 true
+07:00 true
+08:00 true
+09:30 true
+10:00 true
+11:00 true
+12:00 true
+13:00 true
+14:00 true
+200:00 false
-200:00 false
+200:001 false

Upvotes: 0

E.Bekboev
E.Bekboev

Reputation: 1

This regex accepts all formats: +12, -12 and 12

/^[-\+]?(?:(?:1[012]|[0-9])(?:\.\d{1,2})?)$/

https://regex101.com/r/tM8sW0/3

Upvotes: 0

Matt
Matt

Reputation: 20776

valid_timezones = [0, 1, 2, 3.5, 5.75] # Use your array here
regexp = Regexp.new "\\A(#{valid_timezones.join('|')})\\z"
# => /\A(0|1|2|3.5|5.75)\z/ 

I'm assuming that the numeric values are submitted (e.g. "5.75") rather than the timezone strings (e.g. "(GMT +5:45)") so that is what you want to match with the regexp.

Upvotes: 0

Joseph Marikle
Joseph Marikle

Reputation: 78520

Here's one way. Note that it test against .5 and .75 but can't tell the difference between 6.5 and 7.5. Both those numbers would pass, but only one is a valid timezone. This will however test against the right format at least.

/^-?\d{1,2}(\.5|\.75)?$/

Here are some tests:

/^-?\d{1,2}(\.5|\.75)?$/.test("5.5"); // returns true
/^-?\d{1,2}(\.5|\.75)?$/.test("5.7"); // returns false
/^-?\d{1,2}(\.5|\.75)?$/.test("-3.75"); // returns true
/^-?\d{1,2}(\.5|\.75)?$/.test("+05:30"); // returns false

Edit: oops! forgot to make sure it tested for 1 or 2 numbers for the integer value.

Upvotes: 1

vks
vks

Reputation: 67968

^-?\d{1,2}(?:\.\d{1,2})?$

This should do it for you.See demo.

https://regex101.com/r/rU8yP6/8

or

^(?:-?(?:1[01]|[0-9])(?:\.\d{1,2})?|12)$

https://regex101.com/r/rU8yP6/10

Upvotes: 1

Related Questions