SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: date validation

  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    date validation

    I have a form which contains a date input of this form : DD/MM/YYYY

    So i want to validate this form according to this date format.

    Is this possible?

    Thanks in advance!

  2. #2
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: date validation

    Originally posted by jonsof
    Is this possible?
    Not only possible, but free. fValidate
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i found this
    PHP Code:
    function validateDate(formObjdateStrdelimcodespecDate) {
        var 
    objName formatName(formObj);    
        if (
    fv['bok'] && checkBlank(formObj))
            { 
    fv['bok']=false; return true; }
        
        var 
    months = ['January','February','March','April','May','June','July','August','September','October','November','December'];
        var 
    vDate formObj.value;
        var 
    mPlace dateStr.indexOf("m");
        var 
    dPlace dateStr.indexOf("d");
        var 
    yPlace dateStr.indexOf("y");
        var 
    yLength dateStr.lastIndexOf("y") - yPlace 1;
        var 
    dateReg dateStr.replace(/\w/g,"\\d");
        
    delim puncStr(delim);
        
    dateReg dateReg.replace(/-/g,"[" delim "]");
        
    dateReg "^" dateReg "$";
        var 
    day vDate.substring(dPlacedPlace+2);
        var 
    month vDate.substring(mPlacemPlace+2);
        var 
    year vDate.substring(yPlaceyPlace yLength);
        var 
    regex = new RegExp(dateReg);
        var 
    = new Date(months[month-1] + " " day ", " year);
        var 
    today = (specDate == 'today') ? new Date() : new Date(specDate);
        
    today.setHours(0);
        
    today.setMinutes(0);
        
    today.setSeconds(0);
        
    today.setMilliseconds(0);
        var 
    timeDiff today.getTime() - d.getTime();
        var 
    dateOk false;
        switch (
    parseInt(code)) {
            case 
    // Before specDate
                
    dateOk = (timeDiff 0);
                break;
            case 
    // Before or on specDate
                
    dateOk = ((timeDiff 86400000) > 0);
                break;
            case 
    // After specDate
                
    dateOk = (timeDiff 0);
                break;
            case 
    // After or on specDate
                
    dateOk = ((timeDiff 86400000) < 0);
                break;
            default : 
    dateOk true;
            }
        if (!
    regex.test(vDate) || == 'NaN' || !dateOk) {
            return 
    errorProcess2(formObj,1,1,"Please enter a valid date");
            }
        return 
    true;
        } 
    But i think is too big function for my case!
    I just want a script which chekck a string like "DD/MM/YYYY"
    i.e. "13/04/2003" and it contains nubmers between slashes then true else false, something simple

  4. #4
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Try this simple function. It uses regular expression to determine the correct format, and returns true or false depending on the user's input:
    Code:
    <script type="text/javascript">
    function validDate(theDate) {
      var re_date = new RegExp("^\d{1,2}\/\d{1,2}\/\d{4}$");
      var isValid = re_date.test(theDate);
      if (!isValid) {
        alert("Please enter the date in the correct DD/MM/YYYY format.");
      }
      return isValid;
    }
    </script>
    Here's an example of how to use it in your HTML:
    Code:
    <label for="date">Date (must be in DD/MM/YYYY format):</label><input type="text" name="date" onblur="return validDate(this.value);" />

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by jonsof
    i found this
    PHP Code:
    // code 
    That function is from my fValidate validation library, and is not designed to be used outside of fValidate. It does more than just check for a valid date format, but rather verifies that it is a valid date according to a specified time-relation.

    Unless you use all of the fValidate library, the above function won't work, because it relies on at least 5 other functions.

    The example vgarcia gives is exactly what you need to test the validity of a date format.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    onblur

    This checks for true or false on Submit?

  7. #7
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Originally posted by jonsof
    onblur

    This checks for true or false on Submit?
    No, the onblur event will run the function when the user tabs away or clicks away from the form field. I put it in there because my example had only one form field. If you want it to work when the form submits, use the "onsubmit" attribute on the <form> tag.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Posts
    53
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tried your script but the message appeared even i put 18/04/2003 which is the wanted date format

  9. #9
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Did you have any extra spaces in there, or any extra info after the date? If so, then remove the spaces, or try taking off the $ sign at the end (it denotes the end of a string in a regular expression). This may take care of your problem.

  10. #10
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Update:
    Fixed the script. Here's the code:
    Code:
    <script type="text/javascript"> 
    function validDate(theDate) { 
    var re_date = new RegExp("^[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{4}$"); 
    var isValid = re_date.test(theDate); 
    if (!isValid) { 
    	alert("Please enter the date in the correct DD/MM/YYYY format."); 
    } 
    return isValid; 
    } 
    </script>

  11. #11
    SitePoint Enthusiast
    Join Date
    Sep 2003
    Location
    Indonesia
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But If I Have two textbox, which : Start Date and End Date, how to validate between them, so end date won't be less dan start date, in this case I use calendar to input the date,no need to validate the date format again...thxx

  12. #12
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Simple comparison should suffice

    if ( date1 > date2 )
    {
    alert( "date1 is after date2" );
    }
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  13. #13
    SitePoint Enthusiast
    Join Date
    Sep 2003
    Location
    Indonesia
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok thx a lot.


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
  •