javascript - Regular expression timezone -
i need regular expression valid time zone, tried following one.
i'm not sure it.
please me find out wrong in following regular expression.
edited:
here colon , minutes optional. how can change mandatory.
if there no minutes user should input 00 (+05:00). please me solve issue.
var chkzone = "+05:30" if(chkzone .match(/^(z|[+-](?:2[0-3]|[01]?[0-9])(?::?(?:[0-5]?[0-9]))?)$/)) { alert('works out'); } else { alert("time zone wrong") }
the following regex matches timezones mandatory double digit hours/minutes, or letter z
:
/^(?:z|[+-](?:2[0-3]|[01][0-9]):[0-5][0-9])$/
explanation:
^ # start of string (?: # match following non-capturing group: z # either literal z | # or [+-] # plus or minus sign (?: # followed non-capturing group: 2[0-3] # either number between 20 , 23 | # or [01][0-9] # number between 00 , 19 ) # end of inner non-capturing group : # match literal colon [0-5][0-9] # match number between 00 , 59 ) # end of outer non-capturing group $ # end of string
see live on regex101.com.
Comments
Post a Comment