Skip to content

Latest commit

 

History

History
37 lines (27 loc) · 973 Bytes

js_date_validator.md

File metadata and controls

37 lines (27 loc) · 973 Bytes

JavaScript real date validator and date helper

Validates correctnes of given date

Installation

  • copy js_date_validator.js to app/assets/javascripts/date_validator.js

Usage

$("#birthday, #birthmonth").focusout( function() {
    if ( $(this).val().match(/^\d{1}$/) ) {
        if ( $(this).val() === "0" ) { $(this).val("01");
        } else { $(this).val("0" + $(this).val()); }
    }

    if ( $("#birthday").val().match(/^\d{2}$/) && $("#birthmonth").val().match(/^\d{2}$/) ) {
        checkBirthdate();
    }
});

$("#birthyear").focusout( function() {
    var current_year = new Date().getFullYear();

    if ( $(this).val().match(/^\d{2}$/) ) {
        if ( $(this).val() <= (current_year - 2000) ) { $(this).val("20" + $(this).val());
        } else { $(this).val("19" + $(this).val()); }
    }
    checkBirthdate();
});

Documentation