Details about how to verify the time format of js regular expressions in the format of xxxx-xx,
In projects, we often encounter time problems. Sometimes the time plug-in is used to allow users to choose from, but sometimes the time can be input at customers' requirements, how can we determine whether the time format entered by the user is correct and whether the input time is valid?
In this case, you need to use a regular expression. Next, we will briefly describe my verification instance, and take the validation time format as an example:
1. First, we need to obtain the user input;
2. Secondly, we need to verify whether the content entered by the user is in format;
3. Verify that the entered time is valid after the format is correct;
4. If the time format entered by the user is correct and valid, the content in the input box is returned. Otherwise, the user is prompted to enter the time again.
The verification process is as follows:
1. Get user input time:
var bagin = $('.input_one').val();
2. Verify that the time format is correct: (the format of the timestamp is returned for verification, for example: (2017-01-01,7 7,-, 01,-, 01); otherwise, null is returned)
Var bagin_r = bagin. match (/^ (\ d {4}) (-) (\ d {2}) (-) (\ d {2}) $ /); if (bagin_r = null) {alert ("enter the correct start time format, for example, 2017-01-01"); return false ;}
3. Check whether the verification time is valid: (Note: This segment must be placed after the verification time format is complete)
Var B _d = new Date (bagin_r [1], bagin_r [3]-1, bagin_r [5]); var B _num = (B _d.getFullYear () = bagin_r [1] & (B _d.getMonth () + 1) = bagin_r [3] & B _d.getDate () = bagin_r [5]); if (B _num = 0) {alert ("the start time is invalid. Enter the correct Start Time"); return false ;}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.