Jump to content
php.lv forumi

Pārbauda email pareizību


Uldis

Recommended Posts

Varbūt kādam noderēs - skripts pārbauda vai epasta forma ir korekta un vai norādītais hosts ir īsts, piem., andris@inboxx.lv atgriezīs false vērtību.

 

<?php

function check_email($mail_address) {

$pattern = "/^[\w-]+(\.[\w-]+)*@";

$pattern .= "([0-9a-z][0-9a-z-]*[0-9a-z]\.)+([a-z]{2,4})$/i";

if (preg_match($pattern, $mail_address)) {

$parts = explode("@", $mail_address);

if (checkdnsrr($parts[1], "MX")){

echo "The e-mail address is valid.";

// return true;

} else {

echo "The e-mail host is not valid.";

// return false;

}

} else {

echo "The e-mail address contains invalid charcters.";

// return false;

}

}

check_email("INFO@google.co.uk");

?>

Edited by Uldis
Link to comment
Share on other sites

Kāds varētu to skriptu nedaudz noslīpēt tā, lai ietu ar visiem variantiem. =)

 

/^[a-z0-9!#$%&\'*+?^_`{|}~-]+(?:\.[a-z0-9!#$%&\'*+?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/

 

Tas it kā der regex'am. =)

Link to comment
Share on other sites

/^([_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@)+[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,6})$/

 

ja veic šādas nelielas izmaiņas tad strādās gan ar diviem @ un arī .museum

 

bet personiigi es neljauju lietot divus @

 

pārāk maz tādu epastu, lai taisiitu izņēmumus.. :))

 

BEZ diviem vai vairāk @

 

/^[_A-z0-9-]+((\.|\+)[_A-z0-9-]+)*@[A-z0-9-]+(\.[A-z0-9-]+)*(\.[A-z]{2,6})$/

 

priekā :)

Edited by sheps
Link to comment
Share on other sites

es personigi neesmu redzejis mailu ar diviem @

pec standarta saprotu ka valids mails ir "user@abc"@domain.lv

 

RFC invalid e-mail addresses

 

* Abc.example.com (character @ is missing)

* A@b@c@example.com (only one @ is allowed outside quotations marks)

* ()[]\;:,<>@example.com (none of the characters before the @ in this example are allowed outside quotation marks)

Link to comment
Share on other sites

Noteiktos gadījumos e-pasts abrakadarba@lieli.laachi.lv arī ir patiess (tad kad tāds serveris ir uzstādīts un strādā), bet ne vienmēr. Ko tavuprāt darīt ar tādu e-pasta adresi? Vai tikpat labi i.like.billy.gates@inbox.lv ? Kādos gadījumos tāds e-pasts ir valīds tavuprāt?

E-pasta validatoram jāpārbauda e-pasta stringa sintakses valīdums (RFCxyz kura tur atbilstība), nevis vai uz konkrētā hosta ir serveris/akkounts. To lai dara e-pasta sūtīšanas programma.

Link to comment
Share on other sites

Ja godiigi, tad tas filter_var nav iipashi labs, jo vinjapraat ratrijs@localhost ir valiids epasts, kas noteiktos gadiijumos ir tiesa, tachu ne vienmeer :D

 

if( filter_var($mail, FILTER_VALIDATE_EMAIL) && strrchr($mail, '@') != '@localhost' )

 

kaut kā tā ;)

 

Jebkurā gadījumā labāk nekā rakstīt garu funkciju.

Edited by Maaren
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...