Click here to Skip to main content
15,946,342 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
can type textbox for number but that is correct date format using javascript
Posted
Updated 29-Aug-12 1:24am
v2
Comments
Joan M 29-Aug-12 7:23am    
Could you clarify it a little bit?
Do you want to enter only numbers?
Do you want to check a date format?

Assuming that you want to check the input to textbox is a valid date, you can refer the below article
JavaScript Date Validation[^]
 
Share this answer
 
Create a javascript function and call that on form submit event.
Try this:
Javascript:
JavaScript
<script type="text/javascript">
function checkdate(input){
    var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
    var returnval=false
    if (!validformat.test(input.value))
        alert("Invalid Date Format. Please correct and submit again.")
    else{ //Detailed check for valid date ranges
        var monthfield=input.value.split("/")[0]
        var dayfield=input.value.split("/")[1]
        var yearfield=input.value.split("/")[2]
        var dayobj = new Date(yearfield, monthfield-1, dayfield)
        if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
            alert("Invalid Day, Month, or Year range detected. Please correct and submit again.")
        else
            returnval=true
    }
    if (returnval==false) 
        input.select()
    return returnval
}
</script>

HTML:
HTML
<form onSubmit="return checkdate(this.mydate)">
<input type="text" name="mydate" />
<input type="submit" value="submit" /><br />
Valid date format: mm/dd/yyyy<br />
<input type="submit" value="submit" />
</form>



--Amit
 
Share this answer
 
    var date1 = new Date();
        var dob = document.getElementById('<%=txtDob.ClientID%>').value;
        var date2 = new Date(dob);
        var pattern = /^\d{1,2}\/\d{1,2}\/\d{4}$/; //Regex to validate date format (dd/mm/yyyy)
        if (pattern.test(dob)) {
            var y1 = date1.getFullYear(); //getting current year
//            var y2 = date2.getFullYear();
//            var y3 = y1 - y2;        
            return true;
        } else {
            alert("Invalid date format. Please Input in (dd/mm/yyyy) format!");
            return false;
        }
 
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