Looking to create a form validation on email text field. Have previously used validation to ensure correct email is produced.
希望在电子邮件文本字段上创建表单验证。之前已使用验证来确保生成正确的电子邮件。
But here looking to create a more custom rule which allows only emails ending in the format .ac.uk
但这里希望创建一个更自定义的规则,只允许以.ac.uk格式结尾的电子邮件
Here a user would be able to provide any university/college/instituion as long as the last 6 characters in the string = .ac.uk with the general format for the mail as follows: email@university.ac.uk
在这里,用户可以提供任何大学/学院/机构,只要字符串= .ac.uk中的最后6个字符,邮件的一般格式如下:email@university.ac.uk
Solution preferably in PHP, currently looking at employing a rule using the end part in this statement:
解决方案最好在PHP中,目前正在考虑使用本声明中的结尾部分使用规则:
^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$
Making this part *(\.[a-z]{2,3})
relate to the .ac.uk
使这部分*(\。[a-z] {2,3})与.ac.uk有关
many thanks, much appreciated Jeanclaude
非常感谢,非常感谢Jeanclaude
1 个解决方案
#1
1
I would first run the email through filter_var($email, FILTER_VALIDATE_EMAIL)
rather than using a simple regex. It's not perfect (I've found a few edge cases that don't validate correctly) but it works well. Once you know it's a valid email address you can simply trust substr($email, -6) == '.ac.uk'
and be done with it. Something like:
我首先通过filter_var($ email,FILTER_VALIDATE_EMAIL)运行电子邮件,而不是使用简单的正则表达式。它并不完美(我发现了一些无法正确验证的边缘情况),但效果很好。一旦你知道它是一个有效的电子邮件地址,你可以简单地信任substr($ email,-6)=='。ac.uk'并完成它。就像是:
if (filter_var($email, FILTER_VALIDATE_EMAIL)
&& strtolower(substr(trim($email), -6))) === '.ac.uk') {
// Valid
}
#1
1
I would first run the email through filter_var($email, FILTER_VALIDATE_EMAIL)
rather than using a simple regex. It's not perfect (I've found a few edge cases that don't validate correctly) but it works well. Once you know it's a valid email address you can simply trust substr($email, -6) == '.ac.uk'
and be done with it. Something like:
我首先通过filter_var($ email,FILTER_VALIDATE_EMAIL)运行电子邮件,而不是使用简单的正则表达式。它并不完美(我发现了一些无法正确验证的边缘情况),但效果很好。一旦你知道它是一个有效的电子邮件地址,你可以简单地信任substr($ email,-6)=='。ac.uk'并完成它。就像是:
if (filter_var($email, FILTER_VALIDATE_EMAIL)
&& strtolower(substr(trim($email), -6))) === '.ac.uk') {
// Valid
}