javascript - How to restrict user to only enter 12 hour time, with AM and PM -
what want been available allow user enter 12 hour time am/pm in a field.
extra: way can validate 2 fields "from - to", example: if user enter 1pm in field from, field call allow 1pm , forward.
any appertained. thanks
you need code parse string , find problems. logic this
- does contain or pm (case insesensitive)
- does have : , if pull in minutes, if not asume it's hours.
- are there numbers before pm/am? if not it's invalid.
- are of numbers greater 12 or smaller 1?
some usefull functions be.
"string split".split(" "); // split array spaces javascript equivalent php explode()
"find pm".indexof('pm'); // return turns how many characters //away pm left. https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/string/indexof
as slice()
"slice after pm".slice(6); //this slice beginning 6th character. https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/string/indexof
Comments
Post a Comment