SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: date validation

  1. #1
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    date validation

    Code:
    function y2k(number) { return (number < 1000) ? number + 1900 : number; }
    
    function isValidPartialDate (myDate,sep) {
    // checks if date passed is in valid mm/yyyy format
        if (myDate.length == 7) {
            if (myDate.substring(2,3) == sep) {
                var month = myDate.substring(0,2);
                var year  = myDate.substring(3,7);
                var test = new Date(year,month-1);
                if (year == y2k(test.getYear()) && (month-1 == test.getMonth())) {
                    reason = '';
                    return true;
                }
                else {
                    reason = 'valid format but an invalid date';
                    return false;
                }
            }
            else {
                reason = 'invalid spearators';
                return false;
            }
        }
        else {
            reason = 'invalid length';
            return false;
        }
    }
    function isPartialDate(aTextField) {
        myDate = document.forms[0][aTextField].value
        if (isValidDate(myDate,'/'))
         return false;
        else
            return true;
    }
    Hey,
    I am using the above code to validate the date in the mm/yyyy format. it doesnt seem to be working for some reason.
    could someone tell me what I am doing wrong?
    Thanks,
    unregistered1.

  2. #2
    SitePoint Addict Mirek Komárek's Avatar
    Join Date
    Dec 2006
    Location
    Prague
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe is better to split date into three separate input fields, then force user to fill it in some format. After that connect them again on server side. Usually I am using this simple function:
    Javascript:
    Code:
    <script type="text/javascript">
    <!--
    function nextFocus(){
    var kde=document.getElementById('date')
    var co=kde.getElementsByTagName('input')
        for(var i=0;i<2;i++){
            co[i].onfocus=function(){this.value=''}
            co[2].onfocus=function(){this.value=''}
            co[i].onkeyup=function(){
            var max=this.getAttribute('maxlength')
            var next=this.getAttribute('tabindex')
                if(this.value.length>(max-1)){
                   if(!this.value.match(/^[0-9]+$/)){
                      alert('Just numbers please');this.value='';return false;
                   }
                   else{
                      co[next].focus();
                   }
                }
            }
        }
    }
    if(document.childNodes){window.onload=nextFocus}
    //-->
    </script>
    HTML:
    Code:
    <fieldset id="date">
    <legend>Auto next focus, fill with numbers in format mm/dd/yyyy</legend>
    <label for="mm">Month </label><input type="text" size="2" id="mm" tabindex="1" maxlength="2" />
    <label for="dd">Day </label><input type="text" size="2" id="dd" tabindex="2" maxlength="2" />
    <label for="yyyy">Year </label><input type="text" size="4" id="yyyy" tabindex="3" maxlength="4" />
    </fieldset>

  3. #3
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    splitting the fields into independent fields is not an option.

  4. #4
    SitePoint Addict Mirek Komárek's Avatar
    Join Date
    Dec 2006
    Location
    Prague
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK At first change this
    Code:
     if (isValidDate(myDate,'/'))
    There is no such function, just isValidPartialDate

  5. #5
    SitePoint Addict
    Join Date
    May 2001
    Posts
    217
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i feel so stupid!!

    that was it!

    thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •