Validate a field with a prest value

Hi,

I’d like to validate a field by a preset value, like a simple captcha, making the input to match a preset value; i.e. preset value=4 field would have “what is 2+2”, and answer to be typed being 4.

I have this

<form method="POST" action="processing.php">
...
<div class="form-group">
<label for="soluzione">What is the total of 9+7+3</label>
<input type="text" id="soluzione" name="soluzione" placeholder="type solution" required="required" />
</div>
...
</form>

and this

<?php
//MAIL HEADER INFORMATION
$EmailFrom = "site name";
$EmailTo = "email to be sent to";
$Subject = "email subject";

function clean_text_string($string) { // FORCE IT TO ACCEPTABLE ESCAPABLE CHARACTERS ONLY
        $new    = trim(ereg_replace('[^\\' a-zA-Z0-9&!#$%()"+:?/@,_\\.\\-]', '&iquest;', stripslashes($string)));
        $new    = ereg_replace(' +', ' ', $new);
return ( $new );
}

foreach ($_POST as $key => $value) {
   $clean_key   = strtolower(clean_text_string($key));
   if (substr($clean_key,0,1) == '_') continue; // SKIP FIELD NAMES THAT START WITH UNDERSCORE
   $clean_value = clean_text_string($value);
   if ($clean_value == '') continue; // SKIP EMPTY FIELDS
   $clean_post["$clean_key"] = $clean_value;
}

$required = array(); // ADD YOUR FIELDS AS NEEDED
$all_okay = TRUE;
foreach ($required as $key) {
   if ($clean_post["$key"] == '') {
      echo "<br/>$key is a required field\
";
      $all_okay = FALSE;
   }
}

// TEST FOR MISSING INPUT
if (!$all_okay) {
   $referer = $_SERVER['HTTP_REFERER'];
   echo "<br /><strong><a href=\\"$referer\\">Please click to go back, and fill the required fields!</a></strong>\
";
   die();
}

// PREPARE THE DATA
$name = Trim(stripslashes($_POST['nome']));
$lastname = Trim(stripslashes($_POST['lastname']));
$email = Trim(stripslashes($_POST['email']));
$message = Trim(stripslashes($_POST['message']));
$soluzione = Trim(stripslashes($_POST['soluzione']));

// PREPARE EMAIL BODY TEXT
$body = '';
foreach ($clean_post as $key => $value) {
   $body .= $key . ': ' . $value . "\
";
}

// send email
$success = mail($EmailTo, $Subject, $body, "From: <$EmailFrom>");

// redirect to success page
if ($success){
header( "Location: http://site/thankyou.php" );
}

else
{print "There has been a technical problem, please resend, thank you."; }
?>

I tried also this js in the head section of the page, but doesn’t seem to work.

<script>
function validate() {
  if (document.getElementById('soluzione').value.replace(/\\s/g, "").toLowerCase() != "4") {
   alert("Please type the result");
   return false;
  }
}
</script>

Although an integration to the processing php file is preferrable, the js solution would be fine as well.

Thank you

Nevermind, I had forgotten to put onsubmit=“return validate()” in the form.

However if ythere is a php snip to integrate the existing script , and remove the js I’d appreciate.

Thank you