in the project, we often encounter the problem of time, sometimes time is the use of plug-ins, allow the user to choose, but sometimes the requirements of customers, can enter their own time, then we, how to determine the user input the time format is correct, whether the input time of law?

this time need to use regular expressions, then I explain simple examples to verify the time format 2017-01-01 format for example:

1., first of all, we need to get the user input the contents of

2.; secondly, we need to verify whether the contents of the user input format for the 2017-01-01

3.; then, to verify the correct format. Need to verify whether the time input method;

4. if the user input is correct, valid time format, then return to the input box content, or prompt the user to enter.

the following specific verification process, reference:

1. get user input time:

 var bagin = $('.input_one').Val (

2.); verification time format is correct (verified by return timestamp format, for example: (2017-01-012017, - 01, - 01,,). Null

 var) or the bagin_r (= bagin.match / ^ (d{4}) (-) (d{2}) (-) ($/ d{2})); if (bagin_r==null) {alert ("please enter the correct start time format, such as 2017-01-01"); return false;} 

3. time to verify the legality of: (note: this section must be placed in to complete the verification time after

 var b_d=new format) Date (bagin_r[1], bagin_r[3]-1, bagin_r[5]); VAR (b_d.getFullYear) ==bagin_r[1]& (b_num = amp; & (b_d.getMonth) (+1) ==bagin_r[3]& & b_d.getDate (= =) Bagin_r[5]); if (b_num==0) {alert ("start time is not legal, please input the correct start time"); return false;} 

above is the whole content of this article, I hope it will help you learn, and I hope you will support the script home more.

This concludes the body part

This paper fixed link: | Script Home | +Copy Link

Article reprint please specify:Detailed explanation of JS regular expression validation time format xxxx-xx-xx form | Script Home

You may also be interested in these articles!