SitePoint Sponsor

User Tag List

Results 1 to 18 of 18

Thread: date?

  1. #1
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,666
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    date?

    I have a form with a date field I want to ensure is nothing but numbers, but also in the format (mm/dd/yyyy) like 01/13/1978. I'm trying to validate the form before it is submitted so that when it is submitted, that field would be numeroc and in the correct format.I'm trying to make an alert box pop up onchange from the field, then once the fields onchange thing is satisfied, the format is checked and corrected if needed. Is there a way to do this w/out javascript or is javascript needed?
    Thank you
    "Oh, and Jenkins--apparently your mother died this morning."

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    validate before submit

    You should validate user input server-side ALWAYS. If you want to valdate client-side first, this is a good thing for honest users. If you don't want to use javascript, you could try VBScript or Jscript I suppose. Or maybe an applet or ActiveX control. But IMHO javascript is the best/easiest way to enhance an honest user's experience with submitting any format specific forms. Any particular reason you want to avoid using javascript?
    If you are using a text input, maybe you could use select options instead. That way you could restrict the options to what you want.
    That's what I did on my wildflower search page http://www.mittineague.com/wild/search.php I don't think it's a bad choice, but it's up to you if you prefer to use text inputs.

  3. #3
    SitePoint Addict chestertondevelopment's Avatar
    Join Date
    Dec 2005
    Location
    Essex, UK
    Posts
    241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could do it in javascript if you wanted but i don't recommend it. What if somebody disables javascript? You can't validate the input, if you really want to use javascript I would recommend validating on the server-side aswell, but if you're going to do that, why not ditch the javascript?

    Anyway, if you do want to use javascript you would need to attach an onsubmit event handler to the form e.g.
    Code:
    <form name="myform" action="action.php" method="post" onsubmit="return validate()">
    You would then need to create a validate() function. In this function you would need to use regular expressions to check the date is valid. e.g.
    Code:
    function validate() {
     	if (document.getElementById('ID OF FIELD HERE') != regular expression here) {
     	    return false;
     	}
     }
    Please bear in mind I haven't tested the javascript yet.

  4. #4
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,666
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    The only reason I would like to avoid javascript is that what if someone uses the form and javascript isn't enabled?
    Wouldn't that render my code useless for those?

    And the only reason I dont want to restrict the user input there for the date is because what if someone goes away for a few months, then tries to make a review on a date 3 months ago?
    "Oh, and Jenkins--apparently your mother died this morning."

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    date

    Quote Originally Posted by lukeurtnowski
    The only reason I would like to avoid javascript is that what if someone uses the form and javascript isn't enabled?
    That's why I call it an "enhancement". If someone doesn't have javascript enabled it will take an extra trip to the server before they find out they submitted invalid input. And for them the code would be "useless". But with javascript they will know before they submit the form, a big plus. Any other technique of doing client-side validation likewise requires that whatever it is using is enabled, be it Java or ActiveX or whatever. The only way to ensure valid input client-side is to limit the choices. And as said (and can't be said too often) - always validate server-side. To be honest, I don't see how using selects would prevent someone from entering any date for any time period. I could select "June 17 2006" just as easily as "March 10 2006" or any other date no?

  6. #6
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,666
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    k

    Yea, but If I made a combo box, wouldn't I have to write about 90 choices and update it all the time?

    Now that I think of it, I think thats the safest wat to do. Question: Is there a way to generate a combo box with options in the format mm/dd/yyyy dating back from the current date 90 days?
    "Oh, and Jenkins--apparently your mother died this morning."

  7. #7
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    selects

    If you only made 1 select, yes (a lot more than 90 ).
    But you could do like I did my the page I linked to. 1 select for month, 1 for day, and 1 for year if you needed it. Then put them together like you need them to be.
    PHP Code:
    $date $_POST[month "/" $_POST['date'] . "/" $_POST['year']; 

  8. #8
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,666
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    k, I think what would be best if I had them all schrunched into 1 select box.
    The code would look something like this, right?
    HTML Code:
     <label>Date: </label>
     <select name="Date">
    [php]
     <?php
     for($x=0, x==90, x++)
     {
     echo "<option value'??'>"??"</option>";
     }
     
     ?>
    [/php]
     </select>
    But how do I make the 1st option the current date, and then the rest of the 89 the days before then?
    Thanks
    "Oh, and Jenkins--apparently your mother died this morning."

  9. #9
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    90 days

    If you only need/want the last 90 days I would populate an array and use that to fill the options. Maybe something like
    PHP Code:
    $get_today getdate();
    $opt_date[0] = date("F, j, Y",$get_today);
    for(
    $i 1$i 90$i++){
    $opt_date[$i] = date("F, j, Y",$get_today $timestap_val_for_1_day);
    $timestap_val_for_1_day += $timestap_val_for_1_day;

    Then
    PHP Code:
    for($x=0$x<90$x++) {
    echo 
    "<option value='".$x."'>".$opt_date[$x]."</option>";


  10. #10
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,666
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    im getting this error in
    <?php
    for($x=0, $x<90, $x++) {
    echo "<option value='".$x."'>".$opt_date[$x]."</option>";
    }
    ?>
    http://scasp.com/testing/reviewform.php
    Thanks
    "Oh, and Jenkins--apparently your mother died this morning."

  11. #11
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    date? I'm kinda iffy on long distance relationships.. oh PHP date! The code:

    for($x=0, $x<90, $x++) {

    the commas need to be semicolons

  12. #12
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,666
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thanks for pointing out my stupidity (again).
    Made the change, but now I get a new error?
    http://scasp.com/testing/reviewform.php
    here the php code in that area (line 71 is below the for loop)
    PHP Code:
    $get_today getdate();
    $opt_date[0] = date("F, j, Y",$get_today);
    for(
    $i 1$i 90$i++){
    $opt_date[$i] = date("F, j, Y",$get_today $timestap_val_for_1_day);
    $timestap_val_for_1_day += $timestap_val_for_1_day;

    "Oh, and Jenkins--apparently your mother died this morning."

  13. #13
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    date operands

    :d'oh: sorry, you can't subtract an int from an array. getdate() should be mktime().

  14. #14
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,666
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thank you, that works perfect. (Except that the combo box simply shows the current date 90 times) How do I make it show the 90 prevous days and also be the date in the format YYYY-mm-dd?
    Thanks.
    "Oh, and Jenkins--apparently your mother died this morning."

  15. #15
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)

    date

    I don't know how you did your loop. I did a for loop that works OK. I had to change the incrementing line, it was doubling itself. And I suppose a do-while could be used to make the code more efficient. The PHP docs show how to format the date in whatever way you want it, full words, abbreviations, numbers, etc. It's the "F, j, Y" part. For year first, put the "Y" first.
    PHP Code:
    <html><head></head><body>
    <?php
    $get_today 
    mktime();
    $timestamp_val_for_1_day 86400;
    ?>
    <form> 
    <label>Date: </label>
     <select name="Date">
    <?php
    $opt_date
    [0] = date("Y, F, j",$get_today);
    echo 
    "<option value='0'>" $opt_date[0] . "</option>";
    for(
    $i 1$i 90$i++){
    $opt_date[$i] = date("Y, F, j",$get_today $timestamp_val_for_1_day);
    $timestamp_val_for_1_day += 86400;
    echo 
    "<option value='" $i "'>" $opt_date[$i] . "</option>";
    }
    ?>
    </select>
    </form>
    </body>
    </html>

  16. #16
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,666
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    thanks...
    "Oh, and Jenkins--apparently your mother died this morning."

  17. #17
    SitePoint Enthusiast mydream's Avatar
    Join Date
    Oct 2007
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By just reading your post title, I thought you were looking for someone to date.

  18. #18
    SitePoint Wizard lukeurtnowski's Avatar
    Join Date
    Mar 2003
    Location
    Coronado
    Posts
    1,666
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Well that too, it was a double meaning. I am the dateless coder.
    "Oh, and Jenkins--apparently your mother died this morning."


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
  •