validate a .edu or .ac email address

点点圈 提交于 2019-12-13 03:04:17

问题


I'm a noob but trying vigorously to simply validate email addresses that only end in ".edu" or ".ac" is there a simple function/script/solution to this seemingly simple problem? able to use php,javascript or jquery.

Any help would be great thanks in advance!


回答1:


You want a regular expression.

The following pattern tests for any e-mail address ending in a top-level domain like .com, .org, .net, .biz etc.

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|museum)\b

You can use the preg_match function in PHP, pass this as the pattern, and just change the list of top-level domains you want to accept at the end. If the function returns 0, the address didn't validate, if it returns 1, it did match.

Regex source: http://www.regular-expressions.info/email.html

preg_match: http://php.net/manual/en/function.preg-match.php

jQuery also has a validate plugin with built-in patterns for validating e-mail addresses, but you'd need to combine this with the server-side validation in PHP for those people that have Javascript disabled.

Validate plugin: http://docs.jquery.com/Plugins/Validation/validate



来源:https://stackoverflow.com/questions/2513902/validate-a-edu-or-ac-email-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!