Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,447
    Thanks
    71
    Thanked 102 Times in 101 Posts

    Check field for correct format

    For a textbox field called "dob" which is birthday. The format needs to be 0000-00-00
    Which for the sql needs to be YYYY-MM-DD

    How can I check to see if it's in that format and if not show the error? or would it be easier to have 3 textboxes, one for each field and then merge them together for the insert?

    Thanks
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,175
    Thanks
    23
    Thanked 601 Times in 600 Posts
    This is a HTML JS problem. Easiest way for you and the user is to use Datepicker | jQuery UI found here Datepicker | jQuery UI
    Or three dropdowns; one for year - etc.
    Last edited by sunfighter; 04-09-2014 at 02:19 PM.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,447
    Thanks
    71
    Thanked 102 Times in 101 Posts
    There is an easier way of doing it in PHP. That Datepicker won't work for this.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #4
    Regular Coder
    Join Date
    Sep 2002
    Posts
    456
    Thanks
    0
    Thanked 20 Times in 20 Posts
    shouldn't you be able to send the date supplied to the date function and reformat the output
    gmdate("M d, Y", strtotime($Item_DateAdded));
    or something...I mean if you can format what comes out of the database, shouldn't you be able to reverse the process going in?
    NO Limits!! DHCreationStation.com
    ------------------------------------------------------------
    Broken items wanted for tinkerin'! PostItNow@BrokenEquipment.com
    Global Complaint Dept.

  • #5
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,447
    Thanks
    71
    Thanked 102 Times in 101 Posts
    I am thinking explode() code may be helpful. But basically what I am asking is that if the user doesn't type in the correct format (YYYY-MM-DD) then it returns an error. However if it is correct, I know how to insert that.
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #6
    New Coder
    Join Date
    Feb 2014
    Location
    USA
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think YYYY-MM-DD is correct format for SQL.

  • #7
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,447
    Thanks
    71
    Thanked 102 Times in 101 Posts
    Quote Originally Posted by tarquinhall View Post
    I think YYYY-MM-DD is correct format for SQL.
    Not helpful
    Been a sign maker for 7 years. My business:
    American Made Signs

  • #8
    New Coder
    Join Date
    Apr 2014
    Posts
    17
    Thanks
    0
    Thanked 1 Time in 1 Post
    I would use a separate field for each value rather than one field for the full date. Most all websites use the MM/DD/YYYY separate fields format so users will find it easy to enter their date plus it's less prone to error. I would also limit the amount of characters in each html fields' code so that the user can't accidentally enter more than needed. Having separate fields eliminates the need to explode the value to be able to work with each date element. Displaying an error for wrong user input using PHP there are more steps involved than say using JavaScript. Unlike JavaScript which is executed in the users browser (I.e. The user enters data, does some action (clicks the submit button) which activates JavaScript to check the values) PHP is only executed on the web server (I.e when a user request a page from the web server the PHP code is executed and then the resulting page is sent to the users browser). To get PHP to 'process' the data entered by the user, the page must first send the data back to the server where the PHP can check the values. Once the web server has the form data, your receiving PHP code could check for errors and be designed to do 1 of 2 following things.
    1) If an error is detected in user input the sending page containing the date fields could be regenerated by the PHP code containing the date fields with or without the users entered data plus some text formatted to show an appropriate error message to the user explaining the problem.
    2) If the values entered are in a valid format the PHP code can manipulate the values into a mysql DATETIME format and insert to the database.

  • #9
    New Coder
    Join Date
    Apr 2014
    Posts
    17
    Thanks
    0
    Thanked 1 Time in 1 Post
    To check the values entered match a valid date you could use the checkdate() function.


  •  

    Posting Permissions

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