$email = $_POST ["Email"];
$pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^";
if (!preg_match ($pattern, $email) ){
echo $ErrMsg = "Email is not valid.";
} else {
echo "Your valid email address is: " .$email;
}
Question? how to validate an email then it should enter the database. I have to validate every field i have but tell me how to validate one field , rest i can do it.
You have already provided answer, just do this:
$pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^";
if (!preg_match ($pattern, $insertData["userEmail"]) ){
// invalid email
} else {
// valid email
}
and if you're asking how implement validation so you can pass errors to frontend, you can store all errors in $errors
array and check after validation that it should be empty. Then if it's not, pass it to frontend or echo it or whatever you want.
And for preventing from adding to database:
if(count($errors) == 0) {
$insertResult = $this->insert("userregistration", $insertData);
}