Txheeb xyuas Chaw Sau Ntawv Email nrog JavaScript thiab Kab Lus Qhia Uas Niaj Hnub

Ib pliag dhau los kuv tso a Lo Lus zais Siv Yuam Kev Siv Npe Siv JavaScript thiab Kev Qhia Ua Raws CaiCov. Ntawm tib qho ntawv sau tseg, koj tuaj yeem tshawb xyuas tus qauv ntawm tus email chaw nyob uas yog siv cov kev qhia li qub (regex) kev teeb tsa.

Yog tias koj daim ntawv foos muaj lub id = ”emailaddress” thiab koj ntxiv ib daim ntawv onSubmit = ”rov qab checkEmail ();“, Qhov no yog Javascript txoj haujlwm uas koj tuaj yeem siv kom rov qab ceeb toom yog tias email chaw nyob muaj tus qauv tsim nyog lossis tsis:

function checkEmail() {
var email = document.getElementById('emailaddress');
var filter = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if (!filter.test(email.value)) {
alert('Please provide a valid email address');
email.focus;
return false;
}
}

Cov nuj nqi ua haujlwm lees paub cov ntsiab lus ntawm email rau cov uas lim. Yog tias qhov kev sib piv tsis ua tiav, nws pops ceeb toom ntxiv thiab rov qab ua kom pom tseeb rov qab rau email chaw nyob!

41 Comments

  1. 1

    Rau cov foos nrog ntau email chaw nyob, nws yuav zoo ua chav kawm = "emailaddress". Yog tias koj muaj lub tsev nyeem prototype.js (http://www.prototypejs.org) nyob rau ntawm nplooj ntawv koj tuaj yeem ua qee yam zoo li no:

    var siv tau = muaj tseeb;
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    $$ ('. emailaddress') .Txhua (kev ua haujlwm (email) {
    yog (! filter.test (email.value)) {
    qhia (? Thov muab ib tug siv tau email?);
    email.focus;
    siv tau = tsis tseeb;
    }
    });
    rov qab siv tau;

  2. 5
  3. 7

    Kuv nyiam lub tswv yim, tab sis kuv yuav txaj muag txais yuav qhov kev hais qhia tsis tu ncua yam tsis muaj kev piav qhia qhov twg email chaw nyob nws tsis lees txais thiab qhov chaw nyob twg tsis raug cai nws tso cai.

    Rau ib qho piv txwv ntawm kev qhia tawm tsis tu ncua uas ua txoj hauj lwm zoo nrog kev piav qhia txog qhov xwm txheej twg uas nws tsis them, saib qhov no:

    http://www.regular-expressions.info/email.html

    Kuv tus kheej nyiam yog npog tag nrho cov teeb meem yooj yim tshaj plaws thiab tawm lus ceeb toom rau txhua tsav txhua yam es tsis lees nws. Yog hais tias Bob yeej xav nyiag xa bob@com.museum es bob@museum.com, vim li cas tsis cia nws?

    • 8

      Nyob zoo Reg,

      Koj tuaj yeem sim cov Regex siv khiav lag luam an Online Regex Tester.

      Ntxiv thiab, muaj twv yuav raug hu ntau ntau uas tuaj yeem ua tiav yog tias koj xav tau ua ib qho email chaw nyob siv tau raws li RFC.

      Muaj qee qhov laj thawj tsis kam tso cai rau qee tus nkag mus rau email chaw nyob tsis raug:
      1. Lawv yuav npau taws rau koj thaum email uas lawv xav tsis dhau - tsis hais los yog tsis yog koj qhov txhaum qhov chaw nyob tau nkag tsis raug.
      2. Yog tias com.museum yog qhov tseem siv tau thiab, cia peb hais, Yahoo! ua haujlwm nws - txhua qhov chaw nyob email uas bounced yuav muaj kev cuam tshuam zoo rau koj lub tuam txhab lub koob npe rau kev xa email. Qhov no tuaj yeem ua rau tag nrho koj lub tuam txhab email raug thaiv.
      3. Yog tias koj tus muab kev pabcuam email tso cai rau koj nkag mus bob@com.museum, koj yuav tau them rau txhua tus email uas tau xa mus rau tus email chaw nyob kom txog thaum lawv tsis sau npe qhov chaw nyob vim yog bounces. Kuv yuav tshem tawm ntawm txhua qhov ESP uas yuav tso cai rau tus email tsis raug zoo li qhov ntawd - lawv tsuas yog noj koj cov nyiaj xwb!

      Tsaug rau kev siv ceev xwmphem los ntawm!
      Doug

  4. 9
  5. 10

    Muaj ntau txoj kev yooj yim los sau cov kev qhia:
    var regex = /^[a-z0-9\._-]+@([a-z0-9_-]+\.)+[a-z]{2,6}$/i;
    - Nrog kev hloov kho zaum kawg / i tsis tas yuav tsum muaj kev qhia ntawm qhov teeb meem sab saud.
    - Kuv tsis paub ib yam TLD nrog tus lej nyob hauv.
    Ntawm ib sab ntawv ceeb toom, Kuv tso cai rau TLD nrog txog 6 daim npav; cov tshiab tuaj tsis tu ncua thiab koj tsis paub (zoo, somme yav tom ntej sawv daws yuav txawm muaj cov lej hauv nws, Kuv paub).

  6. 11

    Nyob zoo,

    Kuv tabtom siv qhov no nyob rau hauv ib daim ntawv uas muaj tiag tiag hauv lub sijhawm, tab sis qhov no tsis pom tias yuav tsum yog siv tau nyob rau lub sijhawm tiag tiag li koj lo lus zais lub zog xyuas…

    Los sis, Kuv tsuas yog qhov tsis zoo, thiab nws tsis yog rau kuv?

  7. 12

    btw, Kuv nyiam qhov uas koj tau tuaj ntawm no, koj cov tutorials yog qhov yooj yim heev, Kuv yuav txhais tau tias yog bookmarking no ib….

  8. 13
  9. 16
  10. 17

    Tsuas yog kho me me: Cov lus niaj zaus muaj ib qho ntxiv () + thaum kawg. Nws yuav tsum nyeem:

    ^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+[a-zA-Z0-9]{2,4}$

    Nrog rau thawj tus twg txhua qhov ntev TLDs yuav raug lees txais (uas tsis yog intrinsecally tsis ncaj ncees lawm raws li lwm tus tau taw qhia, tab sis yog tias qhov no yog lub hom phiaj qhov kev qhia yuav ua kom luv).

  11. 18

    Koj tuaj yeem piav qhia cov kev qhia ib txwm siv ntawm cov cai no thiab seb nws ua haujlwm li cas? Kuj txog .test - Yog .test nqe lus hais ua ntej hauv javascript los tshawb xyuas tej yam zoo li koj tau ua nyob rau hauv cov cai saum toj no?

  12. 19

    Qhov no yog tus lej luv rau email expression-

    muaj nuj nqi validateEmail (id)
    {
    var emailPattern = /^ sua-zA-Z0-9._- tsim+@s-zA-Z0-9.- tsim +.
    rov qab emailPattern.test (id);

    }
    Deepak Rai
    varanasi

  13. 20

    Qhov no yog tus lej luv rau email expression-

    muaj nuj nqi validateEmail (id)
    {
    var emailPattern = /^ sua-zA-Z0-9._- tsim+@s-zA-Z0-9.- tsim +.
    rov qab emailPattern.test (id);

    }
    Deepak Rai
    varanasi

  14. 21
  15. 22
  16. 23

    Ua tsaug, tab sis muaj ib qho yuam kev hauv no regex. Kuv tsis yog tus kws regex, tab sis kuv sim email:

    twv @ test

    thiab nws dhau lub regex… Kuv pom tias nws tsis muaj qhov khiav dhau ntawm "." yog li nws yuav tsum yog:

    /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/

  17. 24
  18. 27

    Zoo, qhov no tsuas yog ntxhib tab sis tsis 100% muaj tseeb, piv txwv li qhov no yuav zoo nrog john_doe. @ gmail.com uas yog qhov tseeb tsis yog qhov siv tau e-mail chaw nyob (dot tsis raug tso cai raws li lub cim kawg hauv ib feem ntawm chaw hauv e-mail).
    Tsis tas li nws yuav txais john …doe@gmail.com uas kuj tsis raug vim tias yuav tsis muaj ntau dua ib qho nyob hauv kab ua ntu zus.

    Cov no tsuas yog qee qhov tsis pom kev uas kuv pom thaum pom thawj zaug.
    Kuv lub tswv yim tsis yog bashing tsuas yog taw qhia qhov no hauv rooj plaub tus neeg npaj yuav siv qhov no ua kev ruaj ntseg kos - tsis muaj kev ruaj ntseg txaus.

    Rau cov ntaub ntawv hais txog siv tau e-mail chaw nyob xyuas qhov no: http://en.wikipedia.org/wiki/E-mail_address

  19. 28

    Akwj,

    Tiag tiag, Kuv xav tias koj yuav tsum tau thov kev khiav tawm rau lub teev (“.”). Yog li, koj txoj haujlwm yuav tsum yog, hloov:

    muaj nuj nqi validateEmail (id)
    {
    var emailPattern = /^ sua-zA-Z0-9._- tsim+@s-zA-Z0-9.- tsim +.
    rov qab emailPattern.test (id);

    }

    Txwv tsis pub, nws cim tias "txhua tus cim". Kuv ntseeg tias cov cim tshwj xeeb yuav tsum khiav dim.

    regards,

    Federico

  20. 29

    muaj nuj nqi validateEmail (fld) {
    var yuam kev = ””;
    var tfld = vwj (fld.value); // tus nqi ntawm daim teb nrog whitespace txiav tawm
    var emailFilter = / tsis hais lus xa ntawv = muaj ib tus hais tsis raws lis.
    var tsis raug caiChars = / [(),;: \ "[]] /;

    yog (fld.value == "Sau koj email chaw nyob") {

    yuam kev = “Thov sau koj tus email address.n”;
    } lwm tus yog (! emailFilter.test (tfld)) {// kuaj email rau cov cim tsis raug cai

    yuam kev = “Thov sau email chaw nyob uas siv tau.n”;
    } lwm tus yog (fld.value.match (tsis raug caiChars)) {

    yuam kev = “Thov sau email chaw nyob uas siv tau.n”;
    }
    rov yuam kev;
    }

  21. 30

    muaj nuj nqi validateEmail (fld) {
    var yuam kev = ””;
    var tfld = vwj (fld.value); // tus nqi ntawm daim teb nrog whitespace txiav tawm
    var emailFilter = / tsis hais lus xa ntawv = muaj ib tus hais tsis raws lis.
    var tsis raug caiChars = / [(),;: \ "[]] /;

    yog (fld.value == "Sau koj email chaw nyob") {

    yuam kev = “Thov sau koj tus email address.n”;
    } lwm tus yog (! emailFilter.test (tfld)) {// kuaj email rau cov cim tsis raug cai

    yuam kev = “Thov sau email chaw nyob uas siv tau.n”;
    } lwm tus yog (fld.value.match (tsis raug caiChars)) {

    yuam kev = “Thov sau email chaw nyob uas siv tau.n”;
    }
    rov yuam kev;
    }

  22. 31

    muaj nuj nqi validateEmail (fld) {
    var yuam kev = ””;
    var tfld = vwj (fld.value); // tus nqi ntawm daim teb nrog whitespace txiav tawm
    var emailFilter = / tsis hais lus xa ntawv = muaj ib tus hais tsis raws lis.
    var tsis raug caiChars = / [(),;: \ "[]] /;

    yog (fld.value == "Sau koj email chaw nyob") {

    yuam kev = “Thov sau koj tus email address.n”;
    } lwm tus yog (! emailFilter.test (tfld)) {// kuaj email rau cov cim tsis raug cai

    yuam kev = “Thov sau email chaw nyob uas siv tau.n”;
    } lwm tus yog (fld.value.match (tsis raug caiChars)) {

    yuam kev = “Thov sau email chaw nyob uas siv tau.n”;
    }
    rov yuam kev;
    }

  23. 32

    muaj nuj nqi validateEmail (fld) {
    var yuam kev = ””;
    var tfld = vwj (fld.value); // tus nqi ntawm daim teb nrog whitespace txiav tawm
    var emailFilter = / tsis hais lus xa ntawv = muaj ib tus hais tsis raws lis.
    var tsis raug caiChars = / [(),;: \ "[]] /;

    yog (fld.value == "Sau koj email chaw nyob") {

    yuam kev = “Thov sau koj tus email address.n”;
    } lwm tus yog (! emailFilter.test (tfld)) {// kuaj email rau cov cim tsis raug cai

    yuam kev = “Thov sau email chaw nyob uas siv tau.n”;
    } lwm tus yog (fld.value.match (tsis raug caiChars)) {

    yuam kev = “Thov sau email chaw nyob uas siv tau.n”;
    }
    rov yuam kev;
    }

  24. 33
  25. 34
  26. 35
  27. 36
  28. 37
  29. 38
  30. 39
  31. 40

Yuav ua li cas koj xav hais tias?

Qhov Web site no siv Akismet los txo cov kev pabcuam. Kawm li cas koj cov ntaub ntawv tawm tswv yim tiav.