|
|
ru.perl- RU.PERL ---------------------------------------------------------------------- From : Alexander V. Gaiduk 2:5020/400 05 Sep 2001 13:31:40 To : Dave Asatrian Subject : Re: Регексп для проверки e-mail'a -------------------------------------------------------------------------------- > Подскажите плз. рекегскп для проверки валидности введенного е-мэйла. Что-нибудь > продвинутей, чем простая проверка на наличие "@" в строке. ;) Hаверняка ведь с > этой проблемой приходилось сталкиваться - как-то не хочется велосипед > изобретать. ;) а кто то изобретал :)) ########################################### $validaddr = checkaddress($field{$name}); if ($validaddr ne "") { ... } else { print_page("Вы ввели недопустимый адрес: <b>$field{$name}</b><p>"); } sub checkaddress { # yeah, yeah, yeah I know that checking for a valid full RFC-822 e-mail address # without a parser is a damn pain in the ass. # _valid_email tries its best, though. :) # It's gotta be a mailbox. No groups. That's redundant. # You also have to have a full domain name (and all ultimate top domains end in # two or three letters) and _valid_email, unlike the rest of the validation routines, # does not return 1 on success, it returns the e-mail address. Why's that, you ask? # That way we can use _valid_email to simultaneously extract out the # actual jim3@psynet.net from "Jim Thomason"<jim3@psynet.net>, for example. # #Set No_errors to 1 if you don't trust it. my ($email) = @_; $atom = q<[!#$%&'*+\-/=?^`{|}~\w]>; $qstring = q/"(?:[^"\\\\\015]|\\\.)+"/; $word = "($atom+|$qstring)"; $email = _comment_killer($email); $email =~ m/^$word\s*\<\s*(.+)\s*\>\s*$/; #match beginning phrases $email = $2 if $2; #if we've got a phrase, we've extracted #the e-mail address #and stuck it in $2, so set $email to it. #if we didn't have a phrase, the whole thing #is the e-mail address return $1 if $email =~ m< ^\s*($word # any word (see above) (?:\.$word)* # optionally followed by a dot, # and more words, as many times as we'd like @ # and an at symbol $atom+ # followed by as many atoms as we want (?:\.$atom+)* # optionally followed by a dot, and more # atoms, as many times as we'd like \.[a-zA-Z]{2,3})\s*$ # followed by 2 or 3 letters, and # an optional greater than >xo; }; sub _comment_killer { my ($email) = shift; while ($email =~ /\((?:[^()\\\015]|\\.)*\)/) { $email =~ s/\((?:[^()\\\015]|\\.)*\)// }; return $email; }; ############################################ вроде работает... по крайней мере за два года накапливания мыльных адресов не пропустила ни одну бяку, хотя может клиенты попадаются дисциплинированные. :) =GAV --- ifmail v.2.15dev5 * Origin: Demos online service (2:5020/400) Вернуться к списку тем, сортированных по: возрастание даты уменьшение даты тема автор
Архивное /ru.perl/11999dfbf6f40.html, оценка из 5, голосов 10
|