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!