2014-03-30 4 views
0

나는 보통 내 문제를 해결할 수있는 정보가 가득한이 장소에서 질문하지 않습니다. 그러나, 나는 PHP, Javascript 등등에 익숙하지 않다. 그리고 나는 단지 내가 가지고있는이 문제를 파악하는 것처럼 보이지 않는다. 그래서 나는 너희들을 돌보고 도움에 크게 감사 할 것이다.자바 스크립트 전자 메일 유효성 검사

그래서 필자는 전자 메일의 유효성을 검사하는 Javascript 기능으로 작성하려고했지만 동료와 의견 시스템을 가지고 놀고 있습니다. 여기에 다음과 같이 코드 ...

$(function() { 
    //alert(event.timeStamp); 
    $('.new-com-bt').click(function (event) { 
     $(this).hide(); 
     $('.new-com-cnt').show(); 
     $('#name-com').focus(); 
    }); 

    /* when start writing the comment activate the "add" button */ 
    $('.the-new-com').bind('input propertychange', function() { 
     $(".bt-add-com").css({ 
      opacity: 0.6 
     }); 
     var checklength = $(this).val().length; 
     if (checklength) { 
      $(".bt-add-com").css({ 
       opacity: 1 
      }); 
     } 
    }); 

    /* on clic on the cancel button */ 
    $('.bt-cancel-com').click(function() { 
     $('.the-new-com').val(''); 
     $('.new-com-cnt').fadeOut('fast', function() { 
      $('.new-com-bt').fadeIn('fast'); 
     }); 
    }); 

    // on post comment click 
    $('.bt-add-com').click(function() { 
     var theCom = $('.the-new-com'); 
     var theName = $('#name-com'); 
     var theMail = $('#mail-com'); 
     var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9] {2,4})+$/; 

     if (!theCom.val()) { 
      alert('oh! Don`t forget your message!'); 
     } 
     if (!filter.test(theMail.value)) { 
      alert('Please provide a vaild email address'); 
      theMail.focus; 
      return false; 

     } else { 
      $.ajax({ 
       type: "POST", 
       url: "ajax/add-comment.php", 
       data: 'act=add-com&id_post=' + <? php echo $id_post; ? > +'&name=' + theName.val() + '&email=' + theMail.val() + '&comment=' + theCom.val(), 
       success : function (html) { 
        theCom.val(''); 
        theMail.val(''); 
        theName.val(''); 
        $('.new-com-cnt').hide('fast', function() { 
         $('.new-com-bt').show('fast'); 
         $('.new-com-bt').before(html); 
        }) 
       } 
      }); 
     } 
    }); 

}); 

당신은 내가 var에 필터 등을 제공 참조 ...하지만 간단한 용어로 설명하는 바와 같이이며, 최종 결과는 이메일이 유효하지만 때 말을 경고 것이다 유효한 전자 메일을 입력하고 제출하려고하면 경고와 최종 결과가 나오더라도 문제가 발생하지 않습니다.

죄송합니다.이 문제에 대해 거의 알지 못하거나 미처 알지 못해서 죄송합니다. 어쨌든 도움을 드릴 수 있습니다.

다시 도움이 될 것입니다.

감사합니다.

답변

4

유용한 기능입니다.

function validateEmail(email) { 
    var re = /^(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\".+\"))@((\[[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,}))$/; 
    return re.test(email); 
} 


// Example 
if(validateEmail("[email protected]")) 
{ 
    alert("Valid!"); 
}else{ 
    alert("Invalid!"); 
} 
0

여러 입력 유형의 유효성을 검사하려면 parsley.js과 같은 유효성 검사 라이브러리를 사용하는 것이 좋습니다. 나는 파슬리를 선호하지만 몇 가지 라이브러리를 사용할 수 있습니다. (참고 : 파슬리는 jquery를 사용합니다.) 기본적으로

<form id="emailForm" data-parsley-validate> 
    <label for="email">Email * :</label> 
    <input type="email" name="email" required /> 
</form> 

, 파슬리 ".이 필드는 필요합니다. 이것은 유효한 이메일해야한다"와 같은 일반 오류 메시지가 표시됩니다,하지만 당신은 쉽게 메시지를 사용자 지정하거나 옵트 아웃 모든 메시지에서 표시의 수 모든.

data-parsley-error-message="my message" // A global error message for this input 
data-parsley-email-message="my message" // Replace the default parsley email error message for this input 
관련 문제