如何在javascript中验证1977年至1999年范围内的出生日期code here
DOB
function validate() {
var dob = document.getElementById('txtDOB').value;
var pattern = /^()$/;
if (dob == null || dob == "" || !pattern.test(dob)) {
alert('Invalid date of birth');
return false;
} else {
alert('valid');
return true;
}
}
<input type="text" id="txtDOB" />
<input type="button" value="Submit" onclick="validate()" />
答案 0 :(得分:0)
function validate() {
var date = new Date(document.getElementById('txtDOB').value), date = date ? date.getFullYear() : false;
if (date && date >= 1977 && date <= 1999) {
alert('valid');
return true;
} else {
alert('Invalid date of birth');
return false;
}
}
<input type="text" id="txtDOB" />
<input type="button" value="Submit" onclick="validate()" />
function validate() {
var date = document.getElementById('txtDOB').value, date = date.match(/\d{4}/);
if (date && date[0] >= 1977 && date[0] <= 1999) {
alert('valid');
return true;
} else {
alert('Invalid date of birth');
return false;
}
}
<input type="text" id="txtDOB" />
<input type="button" value="Submit" onclick="validate()" />
function validate()
{
var
date = document.getElementById('txtDOB').value,
year = date.match(/\d{4}/),
extract = date.replace(year,'').match(/(\d+)/g)
;
if(date && year && year[0] >= 1977 && year[0] <= 1999 && new Date(extract[1]+'/'+extract[0]+'/'+year[0]) != 'Invalid Date')
{
alert('valid');
return true;
}
else
{
alert('Invalid date of birth');
return false;
}
}
<input type="text" id="txtDOB" />
<input type="button" value="Submit" onclick="validate()" />