In projects, we often encounter time problems. Sometimes we use time plug-ins to let users choose, but sometimes at the request of customers, we can enter the time by ourselves. So, how do we determine whether the time format entered by the user is correct? , is the time entered legal? This time you need to use regular expressions. This article mainly introduces you to the detailed explanation of the js regular expression verification time format xxxx-xx-xx format. I hope it can help you.
Next, I will briefly explain my verification example, taking the verification time format 2017-01-01 format as an example:
1. First, we need to obtain the content entered by the user;
2. Secondly, we need to verify whether the content entered by the user is in the format of 2017-01-01;
3. Then, after verifying that the format is correct, we need to verify whether the entered time is legal;
4. If the user input is correct and legal time format, then return the content in the input box, otherwise prompt the user to re-enter.
The specific verification process is given below for reference:
1. Get the time input by the user:
var bagin = $('.input_one').val();
2. Verify whether the time format is correct: (verify by returning the timestamp format, for example: (2017-01-01, 2017,-,01,-,01), otherwise return null)
var bagin_r = bagin.match(/^(\d{4})(-)(\d{2})(-)(\d{2})$/); if(bagin_r==null){ alert("请输入正确的开始时间格式,如:2017-01-01"); return false; }
3. Verify whether the time is legal: (Note: This paragraph must be placed after the verification time format is completed)
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("开始时间不合法,请输入正确的开始时间"); return false; }
Related recommendations:
bootStrap time formatting operation
The specific process of formatting time in php
The above is the detailed content of js regular expression verification time format example. For more information, please follow other related articles on the PHP Chinese website!