谁能帮我写个js验证生日

JavaScript088

谁能帮我写个js验证生日,第1张

//收集表单里的数据,函数名

function xxx

{

var birth=xxx//这个是你从表单获得的日期

var birthPattern = /\d{4}-\d{2}-\d{2}/

if(!birthPattern.test(birth)){

errorMessage += "The correct form of your birthday should be yyyy-mm-dd.\n"

document.getElementById("birth").value = ""

}

else if((checkDate(birth)==false)||(birth.length>10)){

errorMessage += "Your should enter a logical date.\n"

document.getElementById("birth").value = ""

}

}

//检查日期合法性

function checkDate(s){

var year = s.substr(0,4)

var month = s.substr(5,2)

var date = s.substr(8,2)

var today = new Date()

var curYear = today.getFullYear()

var curMonth = today.getMonth() + 1

var curDate = today.getDate()

var s1 = parseInt(year)

var s2 = parseInt(month)

var s3 = parseInt(date)

var lunar = (s1%400==0)||((s1%100!=0)&&(s1%4==0))

if((s1>curYear)||(s1<1949)||(s1==1949 &&s2<10))

return false

else if((s1==curYear)&&(s2>curMonth))

return false

else if((s1==curYear)&&(s2>curMonth)&&(s3>curDate))

return false

else if(s3>31)

return false

else if(s3<=31){

if((lunar==false)&&(s2==2)&&(s3>28))

return false

if((lunar)&&(s2==2)&&(s3>29))

return false

if((s2==4 || s2==6 || s2==9 || s2==11) &&(s3>30))

return false

}

else

return true

}

用的时候根据情况改改变量名函数名就行了.

function onchange(){

//这里还要加些正则表达式判断一下,判断输入的是身份证格式

var len = document.getElementById("id").value.length

if(len>=13&&len<=15){

var content = document.getElementById("id").value

var str = content.split("")

var a

for(var i=5i<11i++){

a+=str[i]

//把a转换成生日的格式

}

document.getElementgetById("brithdate").value=a

}

}

大致思路是这样的,也可以用AJAX做出来