2014-10-28 6 views
0

이 코드를 작성하여 양식을 제출하면 정상적으로 작동하지만 유효성 검사기를 구현할 수 없지만 양식이 전송되지 않는다는 메시지는 표시되지 않습니다. 비었다.보내기 전에 Ajax 유효성 검사기를 포함하는 방법

나는 수색했으나 할 수 없었다. 누구든지 나를 도울 수 있습니까?

 <script type="text/javascript"> 
     jQuery(document).ready(function(){ 
      jQuery('#newsletter').submit(function(){ 
        var dados = jQuery(this).serialize(); 

        jQuery.ajax({ 
         type: "POST", 
         url: "newsletter_cadastrar.asp?f=1", 
         data: dados, 
         success: function(data) 
         { 
          $('#resultado-form-newsletter').html(data); 
         } 
        }); 

        return false; 
       }); 
     }); 
     </script> 

<input id="nomenewslbase" name="nomenewslbase" type="text"> 
<input id="emailnewslbase" name="emailnewslbase" type="email"> 

감사합니다.

답변

0

당신은 당신이 좋아 할 수있는 JQuery와 유효성 검사 규칙을 적용 찾는 경우 :

<script type="text/javascript"> 
    function ValidationRules() { 
} 

ValidationRules.prototype.initializeSaveValidators = function() { 
    jQuery.validator.addMethod("csProjectName", 
     function(value, element) { 
      if ($.trim($('#txtProjectName').val()) == "") { 
       if ($.trim($(element).val()) != '') 
        return true; 
       else 
        return false; 
      } else 
       return true; 
     }, "Project Name is required"); 

    jQuery.validator.addMethod("csDescription", 
    function (value, element) { 
     if ($.trim($('#txtDescription').val()) == "") { 
      if ($.trim($(element).val()) != '') 
       return true; 
      else 
       return false; 
     } 
     else 
      return true; 
    }, "Description is required"); 
}; 

ValidationRules.prototype.enableSaveValidators = function() { 
    jQuery.validator.classRuleSettings.csProjectName = { csProjectName: true }; 
    jQuery.validator.classRuleSettings.csDescription = { csDescription: true }; 
}; 

ValidationRules.prototype.disableSaveValidators = function() { 
    jQuery.validator.classRuleSettings.csProjectName = { csProjectName: false }; 
    jQuery.validator.classRuleSettings.csDescription = { csDescription: false }; 
}; 
jQuery(document).ready(function() {   
    var validationRules = new ValidationRules(); 
    validationRules.initializeSaveValidators(); 
    validationRules.enableSaveValidators();  
    $("#btnsubmit").click(function() {     
     applyjQueryValidation(); 
     return false; 
    }); 
}); 
function applyjQueryValidation() { 
     var isValid = $('#newsletter').valid(); 
     //here you can manually check for some extra validations 
     if (isValid==true) { 
      alert("valid"); 
      //here is you ajax call 
     }else{ 
      alert("invalid"); 
     }  
} 
</script> 

또한, 당신은 여기에 jquery.validate.js

JSfiddle 작업 예는 포함 할 필요가있다.

+0

친애하는 Sanjeev Rai. 고마워,하지만 코드가 작동하지 않아. 너 나 좀 도와 줄 수있어? –

+0

이전에 Jquery 유효성 검사기가 코드에서 초기화되지 않았습니다. 자, 코드를 편집했습니다. 다시 시도 할 수 있습니다! 작동하지 않으면 주석에 오류를 게시 할 수 있습니까? –

+0

아직 작동하지 않았습니다. 보내지 않으면 비어 있습니다. –

관련 문제