0

I want to validate BirthDate, which should be in "mm/dd/yyyy" format, on the client side.

I have tried it as following, but it is not working properly:

$("#btnUpdateEditCB3").click(function(event) {
    var txtBirthDate = $('#<%= txtBirthDateCB3.ClientID %>').val();
    var txtNickName = $('#<%= txtNickNameCB3.ClientID %>').val();
    if (txtBirthDate != "") {
        if (txtBirthDate.match(/^(?:(0[1-9]1[012])[\/.](0[1-9][12][0-9]3[01])[\/.](1920)[0-9]{2})$/)) {
            alert("Please enter date in mm/dd/yyyy format");
            $('#<%= txtBirthDateCB3.ClientID %>').focus();
            return false;
        }
    }
});
Salman A
  • 262,204
  • 82
  • 430
  • 521
Vegeta
  • 291
  • 2
  • 7
  • 20
  • are you getting any error? remember, client date time format depends on browser culture as well. – Junaid Jan 30 '12 at 10:30

4 Answers4

4

Below links explains the same...see if it helps you.

Validate date using jquery

Validate date format using jquery

devson
  • 990
  • 8
  • 22
3

I recommend that you use the JavaScript Date() object along with regular expressions to validate a date. You can use a variant of this code as follows:

function ValidateCustomDate(d) {
    var match = /^(\d{2})\/(\d{2})\/(\d{4})$/.exec(d);
    if (!match) {
        // pattern matching failed hence the date is syntactically incorrect
        return false;
    }
    var month = parseInt(match[1], 10) - 1; // months are 0-11, not 1-12
    var day   = parseInt(match[2], 10);
    var year  = parseInt(match[3], 10);
    var date  = new Date(year, month, day);
    // now, Date() will happily accept invalid values and convert them to valid ones
    // therefore you should compare input month/day/year with generated month/day/year
    return date.getDate() == day && date.getMonth() == month && date.getFullYear() == year;
}
console.log(ValidateCustomDate("1/01/2011"));  // false
console.log(ValidateCustomDate("01/1/2011"));  // false
console.log(ValidateCustomDate("01/01/2011")); // true
console.log(ValidateCustomDate("02/29/2011")); // false
console.log(ValidateCustomDate("02/29/2012")); // true
console.log(ValidateCustomDate("03/31/2011")); // true
console.log(ValidateCustomDate("04/31/2011")); // false
Community
  • 1
  • 1
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • I like your answer (it even takes into account leap years). Will be using this from now on. – dokgu Jan 05 '16 at 18:28
1

Is there any specific reason for not using datepicker formatting? Suggest using a jquery datepicker where you can set the formats.

jquery date picker date time formatting

Muthu
  • 2,675
  • 4
  • 28
  • 34
0

If you want to validate date in mm/dd/yyyy format using javascript you can use the following snippet of code.

txtdate is having textbox id

Consider the below code.

    function isValidDate(txtdate) {
    var txtDate = "#" + txtdate;
    var dateString = $(txtDate).val();
    var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(19|20)\d{2}$/;
    if (!(date_regex.test(dateString))) {
    alert("Date Must Be in mm/dd/yyyy format");
}}
Deepak Kothari
  • 1,601
  • 24
  • 31