Two JS validation date format and validation examples

Source: Internet
Author: User
Tags validation examples

Example

var a=/^[1-2]d{3}-(0?[ 1-9]|1[0-2])-(0?[ 1-9]| [12] [0-9]|3[0-1]) $/;
if (!) 2006-6-6 ". Match (a)) {
Alert ("date format is not correct!")

}

See a date validation function

<script language= "Web Effects" >
function Check () {
var a=/^[1-2]d{3}-(0?[ 1-9]|1[0-2])-(0?[ 1-9]| [12] [0-9]|3[0-1]) $/;
if (Document.biao.riqi.value.match (a)) {
Alert ("date format is not correct!")
return False
}
Else
return True
}
</script>

And look at a super beautiful date validation function

function IsDate (b) {
var birth = b.split ('-');
var bir = birth[1]+ '/' +birth[2]+ '/' +birth[0];
var d = new Date (BIR);
var dd = D.format (' mm/dd/yyyy ');
var df = dd.split ('/');
if (df[2]!=birth[0]| | df[0]!=birth[1]| | Df[1]!=birth[2]) {
return false;
}
return true;
}
function Isdates (b) {
var a =/^d{4}[-]d{2}[-]d{2}$/;
if (!a.test (b)) {
return false;
}
return true;
}

Let's look at a date validation instance code

<title>javascript Check to see if the date format is correct </title>
<script language= "JavaScript" >
function Check () {
str = form1.txt.value;
str = str.match (/^ (d{2,4}) (-|/) (d{1,2}) (d{1,2}) $/);
if (str = null) {
Alert (' You enter an invalid date format ');
return false;
}else if (str[3]>12 | | str[4]>31) {
Alert ("The date format you entered is invalid");
return false
}else{
Alert ("The date format you entered is valid");
return true;
}


</script> <body>
<form name= "Form1" onsubmit= "return check ( >
<input type= "text" name= "TXT" >
<input type= "Submit" value= "verify" >
</form>
</ Body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.