asked on
<!Doctype Html>
<Html>
<Body>
<input type="text" id="bday"/>
<button onclick="ValiDate()">Validate</button>
<script>
function ValiDate()
{
if (ValiBday == true)
{
alert("true");
}
else
{
alert("false");
}
}
function ValiBday()
{
//I get the value of the input element
var inputValue=document.getElementById('bday').value;
// I create a new var with Regex rule to get only numbers
var pattern =/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/;
//I set a flag boolen to test if the inputValue has RegEx format
var flag=pattern.test(inputValue);
//I create an if condition to check if flag is true or false
if(flag===false){
ValiBday = "false";
return false;
}
//I parse the value to date object time in unix format (ms)
var birthday =Date.parse(document.getElementById('bday').value);
//I check the condition
if(isNaN(birthday))
{
ValiBday = "false";
return false;
}
ValiBday = "true";
}
</script>
</Body>
</Html>