【问题标题】:preg_match help from an input box [duplicate]来自输入框的 preg_match 帮助[重复]
【发布时间】:2013-01-19 06:45:28
【问题描述】:
I need to validate a Date from an input box. 

正则表达式很好,但仅此而已。 首先它的范围在哪里,其次你能帮助完成这项工作吗? 当它验证时,我不希望它回显任何东西,而当它不希望它时 告诉用户他们必须以 mm/dd/yyyy 格式输入。如果您可以在警报框中执行此操作,那就更好了!感谢各位开发者!!! COMPLETE 代码跟在标签后面。你会看到我的 preg_match 被注释掉了。谢谢各位开发者!!!

$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>

【问题讨论】:

  • 首先学习你明显缺乏的 PHP 和/或 JavaScript 的基础知识。格式化您的代码。此外,由于此处涵盖了验证,因此您没有搜索太多答案:stackoverflow.com/questions/12030810/php-date-validation
  • 不要使用正则表达式来验证数值。这是一个已解决的问题。人们已经编写、测试和调试了处理这个问题的代码。每当您遇到其他人过去可能不得不处理的编程问题时,请寻找可以为您解决问题的现有代码。

标签: php regex forms validation preg-match


【解决方案1】:

以下正则表达式匹配 mm/dd/yyyy

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

第一部分,即(0[1-9]|1[0-2]) 检查从0112 的两位数字。

下一部分,即(0[1-9]|1[0-9]|2[0-9]|3(0|1)) 检查从0131 的两位数字。

下一部分,即\d{4} 检查 4 位数字。

部件之间的/ 检查/ 作为分隔符。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多