douyi3632 2018-12-06 00:36
浏览 98

使用模式验证电子邮件地址

You'd use something like;

<input type="email" name="email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$">

So if you wanted to only allow a domain and subdomain in theory you'd use;

<input type="email" name="email" pattern="[a-z0-9._%+-]+@[example]+\.[com]{2,}/@[subdomain]+\.[example].\[com]$">

I only want to allow email responses from example.com and subdomain.example.com OR only allow email responses ending in .co.uk which is most ideal.

Any help would be appreciated.

UPDATE

Using;

pattern="[a-zA-Z0-9-]+@sub.example.co.uk" means that the validation will work.

But using:

pattern="[a-zA-Z0-9-]+@sub.example.co.uk|+@example.co.uk" means that the validation will work but also allows domains like @gmail.com etc.

UPDATE

Here's the code for the form.

<form action="./login.php" method="post">
<input type="email" id="email" minlength="13" maxlength="29" pattern="[a-zA-Z0-9-]+@subdomain@example.co.uk|+@example.co.uk" style="text-transform: lowercase" placeholder="Enter your email address." name="email" required=""><br>
<input type="submit">
</form>

Ideally I'd be able to set a list of allowed domains and specific email addresses so only jon@example.co.uk, igor@example.co.uk and stephen@example.co.uk could submit. Can you match the beginning of the email as an array or the entire thing in the format of $allowedaddresses to save exploding the @ from the domain.

</div>
  • 写回答

1条回答 默认 最新

  • dongyuli0964 2018-12-06 01:35
    关注
    1. Don't try to write a regular expression to validate email addresses because it's virtually impossible. The standard governing email addresses was very loosely-defined and historically people went many wildly different ways with it.
    2. Don't try to pack too much application logic into a regular expression.

    Since you're only trying to match the domain name, which is generally the only reliably sane part of an email address, you can get away with a simply matching the end of a string with a regular expression.

    $allowed_domains = [
        '.co.uk',
        'bar.co.uk'
    ];
    
    $forbidden_domains = [
        'baz.bar.co.uk'    
    ];
    
    // make it a bit easier to manage the lists of allowed/forbidden domains
    function domain_suffix_to_regex($suffix) {
        return sprintf('/.*%s$/', preg_quote($suffix));
    }
    $allowed = array_map('domain_suffix_to_regex', $allowed_domains);
    $forbidden = array_map('domain_suffix_to_regex', $forbidden_domains);
    
    $emails = [
        'example@foo.bar.co.uk',
        'example@bar.co.uk',
        'example@baz.bar.co.uk'
    ];
    
    foreach($emails as $email) {
        $permitted = false;
        foreach($allowed as $expr) {
            if( preg_match($expr, $email) ) {
                $permitted = true;
                echo "$email allowed by expr: $expr
    ";
                break;
            }
        }
        if( $permitted ) {
            foreach($forbidden as $expr) {
               if( preg_match($expr, $email) ) {
                    $permitted = false;
                    echo "$email forbidden by expr: $expr
    ";
                    break;
               }
            }
        }
        var_dump($permitted);
    }
    

    Output:

    example@foo.bar.co.uk allowed by expr: /.*\.co\.uk$/
    bool(true)
    example@bar.co.uk allowed by expr: /.*\.co\.uk$/
    bool(true)
    example@baz.bar.co.uk allowed by expr: /.*\.co\.uk$/
    example@baz.bar.co.uk forbidden by expr: /.*baz\.bar\.co\.uk$/
    bool(false)
    
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后的密码
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面