webForumDet fria alternativet
Logga in / Bli medlem

Kontrol av email adress

JavaScript

1 svar · 251 visningar · startad av rille77

Medlem sedan feb. 200476 inlägg
Trådstart#1

Har denna function som ska kolla om det är en "korrekt" email adress som har fyllts i ett kontaktformulär men det är nåt som är galet med den kan bara inte hitta vad.

Tacksam för hjälp

function validEmail() {
atPos = document.contactForm.epost.value.indexOf("@")
periodPos = document.contactForm.epost.value.lastIndexOf(".")

 if (document.contactForm.epost.value == ""){
   alert("Du måste fylla i din epost adress!");
document.contactForm.epost.focus();
return false;
 }
 if (document.contactForm.epost.value.length < 6){
  alert("Du måste fylla i din epost adress!");
  document.contactForm.epost.focus();
return false;
 }

 if (atPos < 1){
   alert("Du måste fylla i din epost adress!");
   document.contactForm.epost.focus();
   return false;
 }

 if (periodPos < 4){
  alert("Du måste fylla i din epost adress!");
  document.contactForm.epost.focus();
   return false;
 }
 if(periodPos+4 > document.contactForm.epost.value.length){ 
  if(periodPos+3 > document.contactForm.epost.value.length){
   if(periodPos+2 > document.contactForm.epost.value.length){
  alert("Du måste fylla i din epost adress!");
     document.contactForm.epost.focus();
     return false;
}
  }
 }
}
Medlem sedan aug. 20039 340 inlägg
#2

Rekommenderar detta:

 String.prototype.isValidEmail = function(){
  return !!this.match(/^([a-zA-Z0-9\-\_]{1}([a-zA-Z0-9\-_\|.|-|_]*)[a-zA-Z0-9\-\_]{1})@([a-zA-Z0-9\-\_]{1}([a-zA-Z0-9\-\_|.|-|_]*)[a-zA-Z0-9\-\_]{1})\.([a-zA-Z]{2,4})$/)
} 
function validEmail() {
  if (!document.contactForm.epost.value.isValidEmail()){
    alert("Du måste fylla i din e-post-adress!");
    document.contactForm.epost.focus();
  }
}
262 ms totalt · 4 externa anrop · v20260731065814-full.51f67c91
121 ms — deklarationer (db)
0 ms — hämta statistik (cache)
138 ms — hämta tråd, inlägg och bilagor (db)
121 ms — ändringar (db)