SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Jan 2012
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Validating a date input in form

    Hi I've searched the internet for how to validate date inputs on a form that updates my records. There is plenty of information, but I am having trouble putting it all together. I have read a lot of posts about regular expressions, the preg_match() function and the date() function. I wrote the following function to validate the date, but it doesn't work when I key in a date with the wrong format.


    PHP Code:
    function check_date ($approval_date) {
    if(
    preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/'$approval_date)) {
        echo 
    ''.$approval_date.'';
    }else{
        echo 
    'Please enter date in correct format.';
    }



    This is the code for the input:

    PHP Code:
    echo '<tr>

    <td class="heading">Initial Registration Date: </td>

    <td>

    <span class="dates"><input type="text" name="approval_date" size="25" maxlength="25" value="' 
    date("m/d/Y"check_date($approval_date)) . '" /></span>

    <span id="date_format"> (mm/dd/yyyy)</span>

    </td></tr>'


    I placed the function directly above the code for the input.


    As you can see from the input code, I am populating the input from the database and using the date function to put it in a mm/dd/yyyy format.

    I want the user to be able update the date in the database with this form.

    If someone has a chance to take a look, I would be grateful.

    Bill

  2. #2
    Non-Member Max Height's Avatar
    Join Date
    Dec 2011
    Posts
    303
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by lndlyb44 View Post
    PHP Code:
    if(preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/'$approval_date)) { 
    I am populating the input from the database and using the date function to put it in a mm/dd/yyyy format.
    For starters, you're specifying a mm/dd/yyyy format but your regex starts off with a 4 digit number.


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
  •