Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to validate input field using php
    primarykey
    data
    text
    <p>I've a contact form, and the last field is a math question to be answered from preventing spam emails. what is best way to check if its only a number, no other characters, &amp; answer should be 15. Also ff possible, how make the form clear after its been submitted? </p> <p>HTML code:</p> <pre><code>&lt;p id="math"&gt;10 + 5 =&lt;input type="text" name="answerbox" id="answerbox" value="&lt;?= isset($_POST['answerbox']) ? $_POST['answerbox'] : '' ?&gt;"/&gt;&lt;/p&gt; </code></pre> <p>I've tried using ctype_digit function, but no luck, didn't work.</p> <pre><code>if(ctype_digit($answerbox != 15) === true){ $errors[] = "Math answer is not correct."; } </code></pre> <p>Full php code:</p> <pre><code>&lt;?php if(empty($_POST) === false) { $errors = array(); $name = trim($_POST["name"]); $email = trim($_POST["email"]); $subject = trim($_POST["subject"]); $message = trim($_POST["message"]); $answerbox = trim($_POST["answerbox"]); if(empty($name) === true || empty($email) === true || empty($subject) === true || empty($message) === true || empty($answerbox) === true){ $errors[] = '&lt;p class="formerrors"&gt;Please fill in all fields.&lt;/p&gt;'; } else { if (strlen($name) &gt; 25) { $errors[] = 'Your name is too long.'; } if (ctype_alpha($name) === false) { $errors[] = "Your name only should be in letters."; } if(!preg_match("/^[_\.0-9a-zA-Z-]+@([0-9a-zA-Z][0-9a-zA-Z-]+\.)+[a-zA-Z]{2,6}$/i", $email)){ $errors[] = "Your email address is not valid, please check."; } if($answerbox != 15){ $errors[] = "Math answer is not correct."; } if(empty($errors) === true) { $headers = 'From: '.$email. "\r\n" . 'Reply-To: '.$email . "\r\n" . 'X-Mailer: PHP/' . phpversion(); mail('me@mymail.me',$subject,$message,$headers); print "&lt;p class='formerrors'&gt;Thank you for your message, I'll get back to you shortly!&lt;/p&gt;"; } } } ?&gt; &lt;?php if (empty($errors) === false){ foreach ($errors as $error) { echo'&lt;p class="formerrors"&gt;', $error, '&lt;/p&gt;'; } } ?&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload