简体   繁体   中英

Server Side Validation using controller in Core PHP

$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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM