Forum Moderators: coopster
<?php
# Anti-header-injection - Use before mail()
# By Victor Benincasa <vbenincasa(AT)gmail.com>
foreach($_REQUEST as $fields => $value) if(eregi("TO:", $value) || eregi("CC:", $value) || eregi("CCO:", $value) || eregi("Content-Type", $value)) exit("ERROR: Code injection attempt denied! Please don't use the following sequences in your message: 'TO:', 'CC:', 'CCO:' or 'Content-Type'.");
?> if(preg_match('/^([^@]+)@[a-z0-9][a-z0-9\-]{1,62}(\.[a-z]{2,4}|\.[a-z]{2,3}\.[a-z]{2})$/i', $_POST['email'], $out)) {
if(filter_var($out[1], FILTER_VALIDATE_EMAIL, array('flags' => FILTER_NULL_ON_FAILURE))) {
// Send E-mail
} else {
// Error
}
} else {
// Error
}
Is there a way to validate an email address only if that field has been feeled?
$to = '';
if(isset($_POST['email_one']) && !empty($_POST['email_one'])) {
if(preg_match('/^([^@]+)@[a-z0-9][a-z0-9\-]{1,62}(\.[a-z]{2,4}|\.[a-z]{2,3}\.[a-z]{2})$/i', $_POST['email_one'], $out)) {
if(filter_var($out[1], FILTER_VALIDATE_EMAIL, array('flags' => FILTER_NULL_ON_FAILURE))) {
$com = (empty($to))? '' : ', ';
$to .= $com . $out[0];
} else {
// Error
}
} else {
// Error
}
}
<form name="blah" method="post" action="/blah" [b]onsubmit="return paginate(this)"[/b]>
<input type="text" name="email_one" [b]id="email_one"[/b] />
<input type="text" name="email_two" [b]id="email_two"[/b] />
<input type="submit" />
</form>
<script type="text/javascript">
var emailone = document.getElementById("email_one").value;
var emailtwo = document.getElementById("email_two").value;
var emailpattern = "/^([^@]+)@[a-z0-9][a-z0-9\-]{1,62}(\.[a-z]{2,4}|\.[a-z]{2,3}\.[a-z]{2})$/i";
var val = 1;
function paginate(form) {
if(emailone != NULL && emailone != "") {
if(!emailpattern.test(emailone)) {
return false;
alert("Error:\nInvalid E-mail address entered in the first text box.");
val = 2;
}
}
if(val != 2 && emailtwo != NULL && emailtwo != "") {
if(!emailpattern.test(emailtwo)) {
return false;
alert("Error:\nInvalid E-mail address entered in the second text box.");
val = 2;
}
}
}
</script>
[edited by: eelixduppy at 1:10 pm (utc) on Apr 28, 2010]
[edit reason] exemplified [/edit]