contact.php上的验证错误:如何显示多条验证错误消息?

时间:2017-01-10 20:54:15

标签: javascript php html

首先,我是PHP和JavaScript的新手。我目前正在使用MailHandler.php,TMForm.js和我的contact.php文件处理联系表单。我想知道我的消息textarea如何显示多个验证错误(例如,当某些符号未经验证时,谎言#$<>并且最小长度为5个字符,我希望我的textarea显示两个验证错误一个接一个)。

我在TMForm.js

的末尾添加了一个RegExp
regula.custom({
name:'Special' 
,validator:function(){
    return /^[^\$\%\&\|\<\>\#]*$/.test(this.value)
}
})

这是我的contact.php textarea

<label class="message">
<textarea id="message" class="input" name="cf_message"
          placeholder="Insert your message:" 
          data-constraints="@Required @Special @Length(min=5,max=999999)"></textarea>
<span class="empty-message">*This field is required.</span>
<span class="error-message">*The following characters are not allowed: $, %, &, |. <, > y #.</span>

和我的MailHandler.php

        <?php
    session_start();
    $count = 0;
    if(isset($_POST['cf_email'])) {
    $name = $_POST['cf_name'];
    $email = $_POST['cf_email'];
    $message = $_POST['cf_message'];
    $email_to = "email@mail.com";
    $email_subject = "You have a new email: ".$name;

      $_SESSION['nameErr'] = $nameErr;
      $count = $count + 1;
      $_SESSION['emailErr'] = $emailErr;
      $count = $count + 1;
        $_SESSION['messageErr'] = $messageErr;
        $count = $count + 1;

    function clean_string($string) {
       $bad = array("content-type","bcc:","to:","cc:","href");
       return str_replace($bad,"",$string);
     }

    $email_message = "<html><body style='max-width: 800px;'>";
    $email_message .= "<div><h1 style='font-weight: 500px; font-size: 2em; text-align: center; padding: 30px 0; border-bottom: 1px solid #1D547B; color: #2a3d51;' >El mensaje fue enviado por: ".clean_string($name)."\n</h1>";
    $email_message .= "<p style='font-weight: 900px; font-size: 1em; text-align: justify; padding: 20px 0; font-style: italic; color: grey;'>".clean_string($message)."</p>";
    $email_message .= "</div>";
    $email_message .= "</body></html>";

    $headers = 'From: '.$email."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'content-type: text/html; charset=ISO-8859-1\r\n'.
    'X-Mailer: PHP/' . phpversion();

    if ($count > 0) {
    $Message = "The Message wasn not sent. Please verify again.";
    $_SESSION['Message'] = $Message;
    } else {
      $Message = "You message was sent. Thank you.";
      $_SESSION['Message'] = $Message;
      @mail($email_to, $email_subject, $email_message, $headers);
  }
  $_SESSION['count'] = $count;
  header("Location: ../contactenos.php");
  }
  ?>

我的问题是,它是否可以在自己和同一个textarea上显示验证错误?

1 个答案:

答案 0 :(得分:0)

我建议你使用更多的php来安全地返回。 类似......

trim(stripslashes($_POST['cf_message']));

通过这种方式,您可以根据需要为HTML / JS留下错误处理...