-1
I need to validate a Date from an input box. 

The regular expression is fine but that is about it. First where in the scope does it go, and second can you help make this work? When it validates I dont want it to echo anything and when it doesnt I would like it to tell the user that they must enter it in mm/dd/yyyy format. If you can do this in an alert box that would be even better! Thank you fellow developers!!! The COMPLETE code follows the tag. Youll see my preg_match is commented out. Thank you fellow developers!!!

$input = '<input value="Date" type="text" name="date">';

if (preg_match('/^(|(0[1-9])|(1[0-2]))\/((0… $input)) {
echo "";
} else {
echo "You must Re-enter date in mm/dd/yyyy format.";
}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<embed src="Songs/nature.mp3" width=10 height=10 autostart=true repeat=true loop=true></embed>
    <title>Post Song</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>
    <h1>Post New Song</h1>
    <hr />
    <br />
    <body background="Songs/back.jpg">

    <form action= "Postsong.php" method="POST">
    <span style="font-weight:bold">Song:</span>
        <input value="" type="text" name="song" />
    <span style="font-weight:bold">Artist Name:</span>
        <input value="" type="text" name="name" />
    <span style="font-weight:bold">Date:</span>
        <input value="" type="text" name="date" /><br />
        <br />
        <br />

    <input type="submit" name="submit"
        value="Post Song" />
    </form>
    <hr />
    <p>
    <a href="working.php">View Songs</a>
    </p>



    <?php

/*
    $input = '<input value="Date" type="text" name="date">';

    if (preg_match('/^(|(0[1-9])|(1[0-2]))\/((0[1-9])|(1\d)|(2\d)|(3[0-1]))\/((\d{4}))$/', $input)) {
    echo "";
} else {
    echo "You must Re-enter date in mm/dd/yyyy format.";
}

*/

    if (isset($_POST['submit'])) {
        $Song = stripslashes($_POST['song']);
        $Name = stripslashes($_POST['name']);
        $Date = stripslashes($_POST['date']);
        $Song= str_replace("~", "-", $Song);
        $Name = str_replace("~", "-", $Name);
        $Date = str_replace("~", "-", $Date);
        $ExistingSubjects = array();
        if (file_exists(
            "Songs/songs.txt") &&
            filesize("Songs/songs.txt")
            > 0) {
            $SongArray = file(
                "Songs/songs.txt");
            $count = count($SongArray);
            for ($i = 0; $i < $count; ++$i) {
                $CurrSong = explode("~",
                    $SongArray[$i]);
                $ExistingSubjects[] = $CurrSong[0];
            }
        }
        if (in_array($Song, $ExistingSubjects)) {
            echo "<p>The song you entered
                already exists!<br />\n";
            echo "Please enter a new song and
                try again.<br />\n";                        // Do I need another if statement saying if empty echo you need to enter something?...
            echo "Your song was not saved.</p.";
            $Song = "";
        }
        else {
        $SongRecord =
            "$Song~$Name~$Date~\n";
        $SongFile = fopen(
            "Songs/songs.txt",
            "ab");
        if ($SongFile === FALSE)
            echo "There was an error saving your
                song!\n";
        else {
            fwrite($SongFile, 
                $SongRecord);
            fclose($SongFile);
            echo "Your song has been 
            saved.\n";
            $Song = "";
            $Name = "";
            $Date = "";
        }
    }
}
    else {
        $Song = "";
        $Name = "";
        $Date = "";
    }




    ?>
    </body>
nathanchere
  • 8,008
  • 15
  • 65
  • 86
  • 1
    First of all learn basics of PHP and/or JavaScript which you are clearly lacking. Format your code. Also you haven't searched much for the answer since validation is covered here: http://stackoverflow.com/questions/12030810/php-date-validation – andr Jan 19 '13 at 07:12
  • Don't use regexes to validate numeric values. This is a solved problem. People have already written, tested and debugged code that handles this already. Whenever you have a programming problem that others have probably had to deal with in the past, then look for existing code that does it for you. – Andy Lester Jul 23 '13 at 05:22

1 Answers1

0

The following regex matches for mm/dd/yyyy

(0[1-9]|1[0-2])/(0[1-9]|1[0-9]|2[0-9]|3(0|1))/\d{4}

The first part i.e. (0[1-9]|1[0-2]) checks for two digits ranging from 01 to 12.

Next part i.e. (0[1-9]|1[0-9]|2[0-9]|3(0|1)) checks for two digits ranging from 01 to 31.

Next part i.e. \d{4} checks for 4 digits.

/ between the parts checks for / as separator.

Naveed S
  • 5,106
  • 4
  • 34
  • 52