电子邮件验证php [英] Email Validation php
问题描述
基本上,我需要使用包含确切字母的电子邮件来验证它.可以说我的大学有一个ID为x12627353@student.ncirl.ie的学生,我希望验证通过强制该学生在电子邮件开头输入x,然后输入学生证的8个随机数字,最后进行验证需要@ student.ncirl.ie.否则,不允许学生注册并登录该网站.
Basicly i need to validate the email with it containing exact letters. Lets say my college has a student with id x12627353@student.ncirl.ie , i want the validation to force the student to enter x at the start of the email, then 8 random numbers of their student card and for last to have the validation requiring @student.ncirl.ie. Other wise not allowing the students to register and login into the website.
我的代码是:
if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
$error = true;
$emailError = "Please enter valid email address.";
} else {
// check email exist or not
$query = "SELECT userEmail FROM users WHERE userEmail='$email'";
$result = mysql_query($query);
$count = mysql_num_rows($result);
if($count!=0){
$error = true;
$emailError = "Provided Email is already in use.";
}
}
我当时想这样做: \ b [x0-9 ._%+-] + @ student.ncirl.ie \ b
,但我只是php的初学者,而我不是能够实现它.如果有人可以,您能否通过创建示例代码来帮助我?或简短说明.谢谢.
I was thinking to do it this way: \b[x0-9._%+-]+@student.ncirl.ie\b
but im just a beginner in php and im not able to implement it. If anyone would be able, could u give me a hand by creating a sample code ? Or a brief description. Thank you.
推荐答案
您必须深入研究 preg_match()函数.
搜索主题以匹配pattern中给定的正则表达式.
Searches subject for a match to the regular expression given in pattern.
例如:
$email = "x12627353@student.ncirl.ie";
$result = preg_match('/^x[\d]{8}@student\.ncirl\.ie$/', $email);
if ($result) {
...
do some stuff
...
}
在 regex101 上对正则表达式的测试.
The test of the regular expression on regex101.
说明:
^ 在字符串开头声明位置
x 从字面上匹配字符x(区分大小写)
x matches the character x literally (case sensitive)
匹配以下列表中出现的单个字符:
Match a single character present in the list below :
[\ d] {8}
{8} 量词-精确匹配8次
\ d 匹配一个数字(等于[0-9])
\d matches a digit (equal to [0-9])
@student 逐字匹配字符@student(区分大小写)
@student matches the characters @student literally (case sensitive)
.与字符匹配.从字面上看(区分大小写)
. matches the character . literally (case sensitive)
ncirl 与字符ncirl完全匹配(区分大小写)
ncirl matches the characters ncirl literally (case sensitive)
.与字符匹配.从字面上看(区分大小写)
. matches the character . literally (case sensitive)
即与字符匹配,即按字面值(区分大小写)
ie matches the characters ie literally (case sensitive)
$ 在字符串的末尾或行之前断言位置终止符位于字符串的末尾(如果有的话)
$ asserts position at the end of the string, or before the line terminator right at the end of the string (if any)
希望有帮助.
这篇关于电子邮件验证php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!