<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    軟件測試+JAVA

    -- 新手上路
    posts - 13, comments - 7, trackbacks - 0, articles - 0

    Email Address Validation , Email地址驗證代碼

    Posted on 2007-01-07 09:25 RedWolf 閱讀(1610) 評論(1)  編輯  收藏 所屬分類: WEB測試

    Email Address Validation

    ?
    Many email address validators will actually throw up errors when faced with a valid, but unusual, email address. Many, for example, assume that an email address with a domain name extension of more than three letters is invalid. However, new TLDs such as ".info", ".name" and ".aero" are perfectly valid but longer than three characters. Many email address validators fail to take into account that you do not necessarily need a domain name in an email address - an IP address is fine.

    The first step to creating a PHP script for validating email addresses is to work out exactly what is and is not valid. RFC 2822, that specifies what is and is not allowed in an email address, states that the form of an email address must be of the form "local-part @ domain".

    The "local-part" of an email address must be between 1 and 64 characters in length and may be made up in any one of three ways. It can be made up of a selection of characters (and only these characters) from the following selection (though the period can not be the first of these):
    • A to Z
    • 0 to 9
    • !
    • #
    • $
    • %
    • &
    • '
    • *
    • +
    • -
    • /
    • =
    • ?
    • ^
    • _
    • `
    • {
    • |
    • }
    • ~
    • .
    Or, it can be made up of a quoted string containing any characters except "\". Older email addresses may be made up differently, and may contain a combination of the above. The following are all valid as the first part of an email address:
    • dave
    • +1~1+
    • {_dave_}
    • "[[ dave ]]"
    • dave."dave" (Note that this is considered an obsolete form of address - new addresses created should not be of this form, but it is still considered valid.)
    The following, though similar, are all invalid:
    • -- dave -- (spaces are invalid unless enclosed in quotation marks)
    • [dave] (square brackets are invalid, unless contained within quotation marks)
    • .dave (the local part of a domain name cannot start with a period)
    The "domain" portion of the email address can also be made up in different ways. The most common form is a domain name, which is made up of a number of "labels", each separated by a period and between 1 and 63 characters in length. Labels may contain letters, digits and hyphens, however must not begin or end with a hyphen (officially, a label must begin with a letter, not a digit, however many domain names have been registered beginning with digits so for the purposes of validation we will assume that digits are allowed at the start of domain names). A domain name, technically, need be only one label. However in practice domain names are made up of at least two labels, so for the purposes of validation we will check for two. A domain name may not be over 255 characters in total. A domain portion of an email address may also be an IP address, which can in turn be enclosed in square brackets.

    In order to check that email addresses conform to these guidelines, we'll need to use regular expressions. First, we need to match the three possible forms of the local part of an email address, using the two patterns below (we'll add in escape characters later, when we put the function together):

    1. ^[A-Za-z0-9!#$%&'*+-/=?^_`{|}~][A-Za-z0-9!#$%&'*+-/=?^_`{|}~\.]{0,63}$

    1. ^"[^(\|")]{0,62}"$

    We can use the two patterns we've defined here to check for obsolete local parts of email addresses too, saving ourselves from needing a third pattern.

    Next, we need to check the domain portion of the email address. It can either be an IP address or a domain name, so we can use the two patterns here to validate it:

    1. ^\[?[0-9\.]+\]?$

    1. ^[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9](.[A-Za-z0-9][A-Za-z0-9-]*[A-Za-z0-9])+$

    The above pattern will match any valid domain name, but will also match an IP address, so we only need the above to check the "domain" portion of the email.

    Putting it all together gives us the following function. Call it like any normal function, and you will get back a value of "true" if the string entered is a valid email address, or "false" if the input was an invalid email address.

    1. function check_email_address($email) {
    2. // First, we check that there's one @ symbol, and that the lengths are right
    3. if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    4. // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
    5. return false;
    6. }
    7. // Split it into sections to make life easier
    8. $email_array = explode("@", $email);
    9. $local_array = explode(".", $email_array[0]);
    10. for ($i = 0; $i < sizeof($local_array); $i++) {
    11. if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
    12. return false;
    13. }
    14. }
    15. if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
    16. $domain_array = explode(".", $email_array[1]);
    17. if (sizeof($domain_array) < 2) {
    18. return false; // Not enough parts to domain
    19. }
    20. for ($i = 0; $i < sizeof($domain_array); $i++) {
    21. if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
    22. return false;
    23. }
    24. }
    25. }
    26. return true;
    27. }

    Using the function above is relatively simple, as you can see:

    1. if (check_email_address($email)) {
    2. echo $email . ' is a valid email address.';
    3. } else {
    4. echo $email . ' is not a valid email address.';
    5. }

    You can now validate email addresses entered into your site against the specifications that define email addresses (more or less - domain names that start with a number are supposed to be invalid, but do exist).

    Finally, please do remember that because an email looks valid does not mean it is in use. Using a script for validating email addresses is a good start to email address validation, but though it can tell you an email address is technically valid it cannot tell you if it is in use. You might benefit from checking in more depth, for example seeing if a domain name is registered. Even better, fire off an email to the address given by a user and get them to click a link to confirm it is real - the only way to be 100% sure.

    Feedback

    # re: Email Address Validation , Email地址驗證代碼  回復  更多評論   

    2009-02-06 11:31 by 于光明
    不知道
    主站蜘蛛池模板: 四色在线精品免费观看| 国产成人无码免费看片软件| 91黑丝国产线观看免费| 久久精品国产亚洲AV电影| 免费无遮挡无码永久视频| 精品久久久久久久免费加勒比| 亚洲一级毛片中文字幕| 美女裸身网站免费看免费网站| 日本人护士免费xxxx视频| 在线91精品亚洲网站精品成人| 亚洲第一永久AV网站久久精品男人的天堂AV | 每天更新的免费av片在线观看 | 亚洲AV无码国产精品色午友在线 | 亚洲小说区图片区| 久久久久久国产精品免费免费| 亚洲一线产区二线产区区| a级毛片毛片免费观看久潮喷| 日本免费网站观看| 暖暖免费中文在线日本| 亚洲色爱图小说专区| 久久精品免费一区二区| 亚洲精品无码专区| 亚洲深深色噜噜狠狠爱网站| 午夜精品一区二区三区免费视频| 亚洲一区二区三区国产精品无码| 国产婷婷高清在线观看免费| 中文字幕免费在线看电影大全| 亚洲精品午夜视频| 国产在线观看免费完整版中文版| 三级网站在线免费观看| 亚洲男女性高爱潮网站| 免费v片在线观看品善网| 亚洲精品免费视频| 美女又黄又免费的视频| 久久精品国产亚洲AV无码娇色| 精品国产免费一区二区| 嫩草在线视频www免费看| 亚洲av无码专区青青草原| 亚洲AV日韩精品久久久久久久| 日韩在线天堂免费观看| 日韩免费视频一区二区|