2012-07-05 3 views
0

이메일 주소의 유효성을 검사합니다. 그러나 이것은 적절한 정보를 사용하고 있는지 여부를 검증하지 않습니다. .com, .in, .org, .gov 및 .jo에만 유효성을 검사하고 싶습니다. 어떻게해야합니까? 내 코드는 다음과 같습니다.자바 스크립트에서 이메일 주소의 TLD 유효성 확인

function validateConsumerEmail(){ 
    email = document.getElementById('customer_email').value; 
    if ((email == null)||(email == "")){ 
     alert("Please Enter a Valid Consumer Email Address...") 
     document.consumerEmail.customer_email.focus(); 
     return false 
    } 
    if (echeck(email)==false){ 
     email="" 
     document.consumerEmail.customer_email.focus(); 
     return false 
    } 
    if(email != ''){ 
     var splitting = email.split('@'); 
     if(!isNaN(splitting[0])){ 
    alert('Please provide proper email address...'); 
    document.consumerEmail.customer_email.focus(); 
    return false; 
     } 
     if(splitting[0].length<6 || splitting[0].length > 250){ 
    alert('Please provide proper email address...'); 
    document.consumerEmail.customer_email.focus(); 
    return false; 
     } 

    } 
} 

여기서 customer_email은 필드 이름의 ID입니다.

function echeck(str) { 

    var at="@" 
    var dot="." 
    var lat=str.indexOf(at) 
    var lstr=str.length 
    var ldot=str.indexOf(dot) 
    if (str.indexOf(at)==-1){ 
     alert("Please provide valid email ID."); 
     return false 
    } 
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){ 
     alert("Please provide valid email ID."); 
     return false 
    } 

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){ 
    alert("Please provide valid email ID."); 
    return false 
    } 

    if (str.indexOf(at,(lat+1))!=-1){ 
    alert("Please provide valid email ID."); 
    return false 
    } 

    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){ 
    alert("Please provide valid email ID."); 
    return false 
    } 

    if (str.indexOf(dot,(lat+2))==-1){ 
    alert("Please provide valid email ID."); 
    return false 
    } 

    if (str.indexOf(" ")!=-1){ 
    alert("Please provide valid email ID."); 
    return false 
    } 

    return true     
} 
+0

'때는 isNaN (분할 [0]) '확인의 의미는 무엇입니까? 언제 'NaN'일까요? – alex

+1

함수 echeck()의 ​​내용을 볼 수 있습니까? – DaveHogan

+0

분할 [0]. 길이 <6? 길이가 6 자 미만인 첫 번째 부분을 허용하고 싶지 않습니까? me @ 또는 mycase dave @는 유효하지 않습니다. – DaveHogan

답변

0

시도해보십시오. 현재 코드가 모든 종류의 유효하지 않은 전자 메일을 허용하고 유효하지 않은 많은 전자 메일을 허용하므로 전자 메일 주소의 유효성 검사를 위해 실제로 REGEX를 사용하려고합니다.

그렇다면 실제 유효한 전자 메일의 유효성을 검사 할 수있는 것은 논의의 대상이며 오랫동안 계속되어 왔습니다. 내 예제는 this e-mail-matcher REGEX의 수정 된 버전을 기반으로합니다.

나는 또한 코드를 다소 간소화했습니다. 나는 당신이 이것을 제공하지 않았기 때문에 echeck로 전화를하지 않았다. 필요에 따라 다시 추가하십시오.

function validateConsumerEmail(){ 

    //prep 
    var email = document.getElementById('customer_email'), error; 

    //validate 
    if (!email.value || !/^[\w\.%\+\-][email protected][a-z0-9.-]+\.(com|gov|in|jo|org)$/i.test(email.value)) 
     error = 'Please enter a valid e-mail, with the domain .com, .in, .org, .jo or .gov'; 

    //feedback 
    if (error) { 
     email.focus(); 
     return false; 
    } else { 
     //OK - do something here 
    } 

} 

+0

덕분에 많이 ... – Bappa

+0

나는 그것을 더욱 줄였습니다. 오류 검사는 이제 하나의 조건이되었습니다. – Utkanos

+1

tld 확인을 위해 http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1에 제공된 Mozilla의 유지 관리 된 TLD 목록을 사용할 수도 있습니다. –