Home > Web Front-end > JS Tutorial > body text

js regular expression verification time format example

小云云
Release: 2018-02-11 09:48:56
Original
2490 people have browsed it

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();
Copy after login

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;
 }
Copy after login

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;
 }
Copy after login

Related recommendations:

js formats the current time into year, month, day, hour, minute and second format Detailed explanation

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!