Validate Multiple Emails Comma Separated with javascript

后端 未结 9 2140
无人及你
无人及你 2021-01-12 05:03

I want to validate a string which can be an email or multiple emails separated by commas.

For example:

bill.gates@hotmail.com -> TRUE
bill -> FALSE

9条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-12 05:43

    used this for php preg_match_all()

    $pattern = '/([\w+\._%-]+@[\w+\.-]+\.[\w+]{2,4}[^,;\s])/';
    $text = 'abc@efg.com, efg, hij@emg.com, ak@efg asfbd@efg.com ab@sdfs.com abc+efg@pqr.com';
    preg_match_all($pattern, $text, $match);
    var_dump($match);
    

    array(2) {
      [0] =>
      array(5) {
        [0] =>
        string(11) "abc@efg.com"
        [1] =>
        string(11) "hij@emg.com"
        [2] =>
        string(13) "asfbd@efg.com"
        [3] =>
        string(11) "ab@sdfs.com"
        [4] =>
        string(15) "abc+efg@pqr.com"
      }
      [1] =>
      array(5) {
        [0] =>
        string(11) "abc@efg.com"
        [1] =>
        string(11) "hij@emg.com"
        [2] =>
        string(13) "asfbd@efg.com"
        [3] =>
        string(11) "ab@sdfs.com"
        [4] =>
        string(15) "abc+efg@pqr.com"
      }
    }

提交回复
热议问题