javascript - Create numerical restrictions for date; year, month and day -


i'm trying put restrictions on date input field users don't put in exaggerating dates year 3000. found neat solution works. want date in yyyy/mm/dd format year having restriction, mm between 1-12, dd between 1-31, , yy between 1900-2100.

here's jsfiddle, can't work format yyyy/mm/dd. if change dtarrays

dtmonth = dtarray[5]; dtday= dtarray[7]; dtyear = dtarray[1];

year works mm ends being in place of dd. doing wrong? there better ways of accomplish this? last question.. problem seems pretty simple, books on jquery/javascript recommend may able on own?

i think you've misunderstood in dtarray (which isn't great name). it's output of capture groups regex:

/^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/ 

which matches mm/dd/yyyy (where digits) so

  • [1] = m or mm
  • [2] = / or - separator
  • [3] = d or dd
  • [4] = / or - separator
  • [5] = yyyy

they aren't offsets string. (dtarray[0] whole date matched.) modified regex is

/^(\d{4})(\/|-)(\d{1,2})(\/|-)(\d{1,2})$/ 

i.e. 4 digits in first position, you'll get

  • [1] = yyyy
  • [2] = / or - separator
  • [3] = m or mm
  • [4] = / or - separator
  • [5] = d or dd

and so

dtyear  = dtarray[1]; dtmonth = dtarray[3]; dtday   = dtarray[5]; 

(note @ point 3 variables strings, not integers, albeit containing string representations of integer values. javascript being is, they'll coerced integers when try , use them integers.)


Comments

Popular posts from this blog

java - Intellij Synchronizing output directories .. -

git - Initial Commit: "fatal: could not create leading directories of ..." -