Validation hybrid

Hi all,

I am trying to join to pieces of code

the first is very easy

if ($_POST[“Email”] ==“” || $_POST[“Email”] =="your@email.com"){

 header("Location: index.html");
	exit;		

}

how can i combine the function of the above code with something like this below

function is_valid_email($email) {
$result = TRUE;
if(!eregi(“[1]+(\.[_a-z0-9-]+)@[a-z0-9-]+(\.[a-z0-9-]+)(\.[a-z]{2,4})$”, $email)) {
$result = FALSE;
}
return $result;
}

It dosen’t need to echo a message just simply reload the html form that called the php

Any help would be massivly appreciated


  1. _a-z0-9- ↩︎

Read: ereg deprecated


function is_valid_email($email) {
$result = FALSE;

// # is the delimiter, the i after last # is a switch denoting upper/lower case

if(preg_match("#^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$#i", $email)) {
$result = TRUE;
}

return $result;
}

IF your regex is ok (none of this is tested btw) you’d use it like this:


if ($_POST["Email"] =="" || is_valid_email($_POST["Email"] ) ) {
header("Location: index.html");
exit;	
} 

Thanks heaps for your reply

im kinda a noob …regex??

When the field is empty it still connects the form to the location html.

and when its not valid(eg asdf.com) the page just goes blank.

Also when your@email.com is submitted it still refers the loaction html

Though when a valid format is submitted it links to the location html (like i would like)

I know its a long shot but would you help me further, please???

Well I don’t know what the rest of your page is doing and I dont know what your form contains exactly.

Temporarily tell your form to submit to this test page (ie set <form action=“testpage.php” method="“POST”>)

testpage.php


<?php

var_dump($_POST['Email']);

echo '<hr />';

function is_valid_email($email) {
  $result = FALSE;
  if(preg_match("#^[_a-z0-9-]+(\\.[_a-z0-9-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*(\\.[a-z]{2,4})$#i", $email)) {
  $result = TRUE;
  }
return $result;
}  

if ($_POST["Email"] =="" || is_valid_email($_POST["Email"] ) ) {

echo 'Sending you to Location: index.html';
    
} else {

echo 'Showing you the form.... ';

}

Show us the output of that page when Email is not filled in, when its filled in with a non-email “asdfasdf” and when its got a valid email “asdf@asdf.com”.

Sorry but I’m not in a position to test any of this, just guessing at the code, which is not ideal …

Slight modification…

if ($_POST["Email"] =="" || !(is_valid_email($_POST['Email']) ) ) {
echo 'Showing you the form.... ';
} else {
	echo 'Sending you to Location: index.html';
}