我已经在网上搜索和搜索过堆栈溢出,以获得一些指导,但没有什么与我的代码非常吻合。我正在使用PHP对我的用户注册表单进行一些验证,如下面所示,我还想检查用户注册的电子邮件是否以.ac.uk结尾
// We check for any empty inputs.
if (empty($firstName) || empty($lastName) || empty($username) || empty($email) || empty($password) || empty($passwordRepeat)) {
header("Location: ../signup.php?error=emptyfields&username=".$username."&email=".$email);
exit();
}
// We check for an invalid username AND invalid e-mail.
else if (!preg_match("/^[a-zA-Z0-9]*$/", $username) && !filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: ../signup.php?error=invalidunamemail");
exit();
}
// Check for an invalid username. In this case ONLY letters and numbers allowed.
else if (!preg_match("/^[a-zA-Z0-9]*$/", $username)) {
header("Location: ../signup.php?error=invalidcharacters");
exit();
}
// Check for minimum amount of characters in username.
else if (strlen($username) <7 ) {
header("Location: ../signup.php?error=invalidlengthuname");
exit();
}
// Check for an invalid e-mail. <-- Here is where I also want to add the check of .ac.uk at the end of the email
else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: ../signup.php?error=invalidemail");
exit();
}
// Check if the repeated password is NOT the same.
else if ($password !== $passwordRepeat) {
header("Location: ../signup.php?error=passwordcheck&uname");
exit();
}
else {
// Login发布于 2020-02-24 13:11:05
您可以添加preg_match来检查,但如果条件1有效,则可以跳过第二个条件。这很奇怪,但是如果单独添加,它就能工作了。
// Check for an invalid e-mail. <-- Here is where I also want to add the check of .ac.uk at the end of the email
if ( !filter_var($email, FILTER_VALIDATE_EMAIL) && preg_match('/ac.uk$/', $email) ) {
header("Location: ../signup.php?error=invalidemail");
exit();
} 这个会有用的
// Check for an invalid e-mail. <-- Here is where I also want to add the check of .ac.uk at the end of the email
if ( !filter_var($email, FILTER_VALIDATE_EMAIL)) {
header("Location: ../signup.php?error=invalidemail");
exit();
}
if (!preg_match('/ac.uk$/', $email)) {
header("Location: ../signup.php?error=invalidemail");
exit();
}发布于 2020-02-24 13:17:55
正则表达式到电子邮件Regex匹配特定域
^[a-zA-Z0-9_.+-]+@(?:(?:[a-zA-Z0-9-]+\.)?[a-zA-Z]+\.)?(yourdomain)\.ac.uk$这里是更多的例子
UPDATE:在您的评论中,首先检查扩展,如果它是可接受的域,然后验证。
$email = 'test@domain.ac.uk';
$extention = pathinfo($email, PATHINFO_EXTENSION);
if($extension == ".ac.uk"){
//Do your validation here
filter_var($email, FILTER_VALIDATE_EMAIL));
}else{
//Invalid extention Reject email
}https://stackoverflow.com/questions/60376245
复制相似问题