Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
re = /^(\d{1,2})-(\d{1,2})-(\d{4})*$/;
    if((form.dob.value="") || (!re.test(form.dob.value.match))) {
        alert("Error:enter Valid date of birth!");
        form.dob.focus();
        form.dob.style.background = "yellow";
        return false;
        }

this is my code when i enter date 10-10-2010 or suh type. it does not accept it
Posted
Updated 10-Aug-14 22:22pm
v2

1 solution

Quite a number of errors observed, just to mention a few:
1. syntax error like equal comparison should "==" not "=",
2. regex error: why having an "*" towards the end of the re pattern, "*" means none or more, which means it will accept "10-10-20142012" as well as "10-10-"
Check out this example:
XML
<!DOCTYPE html>
<html>
<body>

<form onsubmit="return validate();" action="">
  Date of Birth: <input type="text" name="dob" value=""><br>
  <input type="submit" value="Submit">
</form>

<script>
function validate()
{
    var dob = document.forms[0].dob;
    re = /^(\d{1,2})-(\d{1,2})-(\d{4})$/;

    if((dob.value == "") || (!dob.value.match(re))) {
        alert("Error:enter Valid date of birth!");
        dob.focus();
        dob.style.background = "yellow";
        return false;
    }
}
</script>

</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900