2016-01-20 479 views
0

我想在表單提交完整性和正確性檢查時替換表單中的提交後檢查,這些檢查是在表單域失去焦點時執行的。如何執行提交表單驗證

我該怎麼做?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!DOCTYPE HTML> 
 
<html> 
 
    <head> 
 
    <title>Form</title> 
 
    <style> 
 
body {  
 
    width: 500px; 
 
} 
 
.part { 
 
    width: 100%; 
 
    padding: 5px; 
 
    border-bottom: 1px solid #000; 
 
} 
 
label { 
 
    margin-right: 5px; 
 
} 
 
.label-left { 
 
    text-align: right; 
 
} 
 
.label-right { 
 
    text-align: left; 
 
} 
 
.error { 
 
    color: #cc0000; 
 
} 
 
    </style> 
 
    <script src="http://code.jquery.com/jquery-latest.min.js"></script> 
 
    <script> 
 
     //$(document).ready(function() { 
 

 
\t function myValidateEMailAddress(email_address) { 
 
     
 
    var email_pattern = /^([\w-\.][email protected]([\w-]+\.)+[\w-]{2,4})?$/; 
 
    return email_pattern.test(email_address); 
 
    } 
 
    
 
    function checkPassword(pwd_str) { 
 
    var my_pwd_pattern = /^(?=.*[a-zA-Z].*[a-zA-Z])(?=.*\d.*\d)[a-zA-Z0-9_]{6,20}$/; 
 
    return my_pwd_pattern.test(pwd_str); 
 
} 
 
function validatePhoneNumber(phone_number) { 
 
     
 
    var phone_pattern = /^(\(?\+?[0-9]*\)?)?[0-9_\- \(\)]*$/; 
 
    return phone_pattern.test(phone_number); 
 
    } 
 

 
    $(document).ready(function() {  
 
    $('#form').submit(function(e) { 
 
     var my_errors = false; 
 
     $('.part> .error').remove(); 
 
     $('#my_submission').empty(); 
 
     $(':text, :password, textarea').each(function() { 
 
     $(this).val($.trim($(this).val())); 
 
     if ($(this).val() == '') { 
 
      $(this).parent().append('<div class="error">Please provide a value</div>'); 
 
      my_errors = true; 
 
     } 
 
     }); 
 
     if ($('#email').val() != '') { 
 
     if (!myValidateEMailAddress($('#email').val())) { 
 
      $('#email').parent().append('<div class="error">Please provide a correct e-mail address</div>'); 
 
      my_errors = true; 
 
     } 
 
     } 
 
\t if ($('#your_password').val() != '') { 
 
     if (!checkPassword($('#your_password').val())) { 
 
      $('#your_password').parent().append('<div class="error">Please provide a correct password.</div>'); 
 
      my_errors = true; 
 
     } 
 
     } 
 
\t if ($('#phone').val() != '') { 
 
     if (!validatePhoneNumber($('#phone').val())) { 
 
      $('#phone').parent().append('<div class="error">Please provide a correct phone number.</div>'); 
 
      my_errors = true; 
 
     } 
 
     } 
 
     if ($('#addresses option:selected').val() == '') { 
 
     $('#addresses').parent().append('<div class="error">Please select one item</div>'); 
 
     my_errors = true; 
 
     } 
 
\t if ($(':radio[name="sex"]:checked').length == 0) { 
 
     $(':radio[name="sex"]:first').parent().after('<div class="error">Please select one item</div>'); 
 
     my_errors = true; 
 
     } 
 
\t if ($(':radio[name="subscription"]:checked').length == 0) { 
 
     $(':radio[name="subscription"]:first').parent().after('<div class="error">Please select one item</div>'); 
 
     my_errors = true; 
 
     } 
 
     if ($('#likes option:selected').val() == '') { 
 
     $('#likes').parent().append('<div class="error">Please select one item</div>'); 
 
     my_errors = true; 
 
     } 
 
     if (my_errors) { 
 
     return false; 
 
     } 
 
     else { 
 
     e.preventDefault(); 
 
     var my_submission_array = $('#form').serialize().split('&'); 
 
     if (my_submission_array.length > 0) { 
 
      $('#my_submission').html('<h2>Submitted Elements</h2><ul></ul>'); 
 
      for (var i = 0; i < my_submission_array.length; i++) { 
 
       var my_pair = my_submission_array[i].split('='); 
 
       $('#my_submission > ul').append('<li>' + my_pair[0] + ': ' + my_pair[1] + '</li>\n'); 
 
      } 
 
     } 
 
     } 
 
    }); 
 
    }); 
 

 
    // }); 
 
    </script> 
 
    </head> 
 
    <body> 
 
    <h3>Output:</h3> 
 
<h2>My Questionnaire</h2> 
 
<form name="form" id="form" action="" method="post"> 
 
<div class="part"> 
 
    <label for="addresses" class="label-left">How should you be addressed?</label> 
 
    <select name="addresses" id="addresses"> 
 
     <option value="">Please select one</option> 
 
     <option value="first">Mr.</option> 
 
     <option value="second">Madam</option> 
 
     <option value="third">Miss</option> 
 
     <option value="fourth">Dr.</option> 
 
\t <option value="fifth">Pr.</option> 
 
    </select> \t 
 
    </div> 
 
    
 
    <div class="part"> 
 
    <fieldset> 
 
     <legend>Sex:</legend> 
 
     <input type="radio" name="sex" id="group1" value="1"> 
 
     <label for="group1" class="label-right">Male</label> 
 
     <input type="radio" name="sex" id="group2" value="2"> 
 
     <label for="group2" class="label-right">Female</label> 
 
    </fieldset> 
 
    </div> 
 
    <div class="part"> 
 
    <label for="last_name" class="label-left">Last Name: </label> 
 
    <input type="text" name="last_name" id="last_name"> 
 
    </div> 
 
    <div class="part"> 
 
    <label for="first_name" class="label-left">First Name: </label> 
 
    <input type="text" name="first_name" id="first_name"> 
 
    </div> 
 
    <div class="part"> 
 
    <label for="email" class="label-left">E-Mail: </label> 
 
    <input type="text" name="email" id="email"> 
 
    </div> 
 
    <div class="part"> \t 
 
\t \t <label for="your_password">Password:</label> 
 
\t \t <input type="password" name="your_password" id="your_password" size="10" maxlength="20"> \t 
 
\t </div> 
 
\t <div class="part"> 
 
    <label for="phone" class="label-left">Phone number: </label> 
 
    <input type="text" name="phone" id="phone"> 
 
    </div> 
 
    <div class="part"> 
 
    <label for="likes" class="label-left">What are your likes?</label> 
 
    <select name="likes" id="likes"> 
 
     <option value="">Please select one</option> 
 
     <option value="first">Programming</option> 
 
     <option value="second"> African literature</option> 
 
     <option value="third">Poetry</option> 
 
     <option value="four">Dancing</option> 
 
    </select> \t 
 
    </div> 
 
    <div class="part"> 
 
    <fieldset> 
 
     <legend>Do you want to receive our newsletter ?</legend> 
 
     <input type="radio" name="subscription" id="group1" value="1"> 
 
     <label for="group1" class="label-right">Yes</label> 
 
     <input type="radio" name="letter" id="group2" value="2"> 
 
     <label for="group2" class="label-right">No</label> 
 
    </fieldset> 
 
    </div> 
 
    <div class="part"> 
 
    <label for="comments" class="label-left">Write some comments below:</label> 
 
    <textarea name="comments" id="comments" cols="40" rows="3"></textarea> 
 
    </div> 
 
    <div class="part"> 
 
    <input type="submit" name="submit" id="submit" value="Submit Form"> 
 
    </div> 
 
    <div id="my_submission"></div> 
 
</form> 
 
    </body> 
 
</html>

+0

我嘗試了一些,但模糊不適用於任何元素,我不知道爲什麼。 – mimireine

回答

0

之前,我繼續回答,我要指出,你把jQuery腳本之前<html>標籤。這是不正確的。它必須是在<head>

<head> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
... 

執行時,表單字段失去焦點

的元素失去當你點擊遠離它的焦點。 jQuery的happends有此場合blur事件:

$('input').on('blur', function() { 
    // your code here 
}); 

所以,你可能想要做這樣說:

$('#email').on('blur', function() { 
    var emailVal = $(this).val(); 
    if (!emailVal || !myValidateEMailAddress(emailVal)) { 
     $(this).parent().append('<div class="error">Please provide a correct e-mail address</div>'); 
     my_errors = true; 
    } 
}); 

的代碼的其餘部分可能類似於。

+0

非常感謝。我將分別爲他們分別研究它們是如何獨立工作的。 – mimireine