Wite a Javascript program for the validation case using the RegExp method. Valid
ID: 3551735 • Letter: W
Question
Wite a Javascript program for the validation case using the RegExp method.
Validate Date of Birth
-The user must enter a DOB in the format mm/dd/yyyy/
-The validation is a two-step process as described below.
-Your program must validate the format first as follows:
-The part "mm", "dd", and "yy" cannot be "00", "00", and "0000" respectively.
-The part "mm" must be two-digits such as "01 through 09" or "10 or 11 or 12".
-The part "dd" must be two-digits such as "01 through 09" or "10 through 31".
-The part "yyyy" must be between "1900 through 2099" inclusive.
-Once the format has been validated, the next step is to verify the following:
-The year entered cannot be greater than the current year (e.g., 2014)
-If the year entered is the current year, then the day entered cannot be greater than today's date.
-The date entered must follow these rules, <= if the month is February, <=30 if the month entered is: September, April, June, and November. <=31 for the remaining months.
Full Rating given to whomever can provide the javascript code to satisfy these requirements in the RegExp format.
**Tutoring Spam will be flagged**
Explanation / Answer
<input type="text" id="dob" />
<input type="button" value="submit"/>
<script>
function validateDob(){
var months = new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);
var str = document.getElementById("dob").value;
var rgx = /(d{2})/(d{2})/(d{4})/;
var mat = str.match(rgx);
var m = new Array();
m[1] = parseInt(mat[1]);
m[2] = parseInt(mat[2]);
m[3] = parseInt(mat[3]);
//alert(m[1]+" "+m[2]+" "+m[3]);
//check for valid month
if(m[1]<1 || m[1]>12){
alert("invalid month");
return false;
}
//check for year
if(m[3]<1900 || m[3]>2099){
alert("invalid year");
return false;
}
if(m[3]>(new Date().getFullYear())){
alert("Wrong year");
return false;
}
else if(m[3]==(new Date().getFullYear())){
if(m[1]>(new Date().getMonth()+1)){
alert("wrong month");
return false;
}
else if(m[1]==(new Date().getMonth()+1)){
if(m[2]>(new Date().getDay())){
alert("wrong date");
return false;
}
}
}
//check for correct no. of days in each month
if(m[1] == 2){
if(m[3]%4 == 0 && m[2]<=29) ;
else if(m[2]<28) ;
else{
alert("February in "+m[3]+" does not have "+m[2]+" day");
return false;
}
}
else if(m[2] > months[m[1]]){
alert("wrong day");
return false;
}
alert("success");
return true;
}
</script>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.