내 사이트에 양식이 있습니다. 제출 버튼을 클릭하면 함수가 실행되어 각 필드의 내용을 가져 와서 RegEx 및 기타 요구 사항에 따라 내용의 유효성을 검사 한 다음 모든 것이 정상적으로 보일 경우 양식이 Ajax를 통해 .php 메일러로 제출됩니다.Javascript 입력 변경시 유효성 확인, 제출 버튼 클릭시 제출
양식이 입력 필드 값이 변경 될 때 자동으로 유효성을 검사하거나 입력 필드가 포커스를 잃을 때 (나는 기본 설정이 없거나 중대 할 때) 양식을 제출할 때만 양식을 제출하기를 원합니다. 사용자가 양식의 제출 단추를 클릭합니다. 모든 필드가 유효하기 전에 사용자가 제출 버튼을 클릭하면 양식을 제출하고 싶지 않습니다.
여기 내 코드입니다 :
//contact form validation
$(function(){
$('.error').hide();
// hide error messages onchange
$("#contact-form").change(function(){
$('.error').hide();
$("label#name_error").hide();
$("label#invalid_error").hide();
$("label#email_error").hide();
$("label#invalid_phone_error").hide();
$("label#phone_error").hide();
$("label#zip_error").hide();
});
// validate fields on click
$(".cool-button").click(function(){
$('.error').hide();
$("label#name_error").hide();
$("label#invalid_error").hide();
$("label#email_error").hide();
$("label#invalid_phone_error").hide();
$("label#phone_error").hide();
$("label#zip_error").hide();
var name=$("input#contact_name").val();
var email=$("input#contact_email").val();
var emailRegEx =/^([a-zA-Z0-9])(([a-zA-Z0-9])*([\._-])?([a-zA-Z0-9]))*@(([a-zA-Z0-9\-])+(\.))+([a-zA-Z]{2,4})+$/
var phone=$("input#contact_phone").val();
var phone=phone.replace(/\s+-/g, "");
var phoneRegEx = /^(1-?)?\s?(\([2-9]\d{2}\)|\s?-?[2-9]\d{2})-?\s?[2-9]\d{2}-?\d{4}$/
var zip=$("input#contact_zip").val();
var best_time=$("input#contact_best_time").val();
var message=$("textarea#contact_message").val();
var timestamp=$("input#timestamp").val();
if(name==""){
$("label#name_error").show();
$("input#contact_name").focus();return false;
}
if(email==""){
$("label#email_error").show();
$("input#contact_email").focus();return false;
}
if (document.getElementById('contact_email').value.search(emailRegEx)==-1) {
$("label#invalid_error").show();
$("input#contact_email").focus();return false;
}
if(phone==""){
$("label#phone_error").show();
$("input#contact_phone").focus();return false;
}
if (document.getElementById('contact_phone').value.search(phoneRegEx)==-1) {
$("label#invalid_phone_error").show();
$("input#contact_phone").focus();return false;
}
if(zip==""){
$("label#zip_error").show();
$("input#contact_zip").focus();return false;
}
if(message==""){
$("label#message_error").show();
$("textarea#contact_message").focus();return false;
}
else {
var dataString='contact_name='+name+'&contact_email='+email+'&contact_phone='+phone+'&contact_zip='+zip+'&contact_best_time='+best_time+'×tamp='+timestamp+'&contact_message='+encodeURIComponent(message);
$.ajax({
type:"POST",
url:"php/contact.php",
data:dataString,
success:function(){$('#contact-form').html("<div id='message'></div>");$('#message').html("<h3>Message Sent</h3>").append("<p>Thank you for contacting us. We'll be in touch. <br /><br />If you have any further questions, you can always email us at [email protected] or call us at 800-924-9473</p>").hide().fadeIn(800,function(){$('#message').append("<img id='checkmark' src='images/submit2.png' />");});}});
}
return false;
});
});
여기에 양식
<form id="contact-form" method="post" action="#">
<p class="form-subscr-field">
<label for="contact_name" id="contact_name_label">Your name: *</label>
<input id="contact_name" type="text" name="contact_name" class="inputbox" size="10" required/>
<label class="error error-tip" for="contact_name" id="name_error" style="display: none; ">Please enter your name.</label>
</p>
<p class="form-subscr-field">
<label for="contact_email" id="contact_email_label">E-mail Address: *</label>
<input id="contact_email" type="email" name="contact_email" class="inputbox" size="10" required/>
<label class="error error-tip" for="contact_email" id="email_error" style="display: none; ">Please enter your email address.</label>
<label class="error error-tip" for="contact_email" id="invalid_error" style="display: none; ">Invalid email address.</label>
</p>
<fieldset class="w50">
<p class="form-subscr-field rowElem left">
<label for="contact_phone" id="contact_phone_label">Phone: *</label>
<input id="contact_phone" type="tel" name="contact_phone" class="inputbox" size="10" required minlength="9"/>
<label class="error error-tip" for="contact_phone" id="phone_error" style="display: none; ">Please enter your phone number.</label>
<label class="error error-tip" for="contact_phone" id="invalid_phone_error" style="display: none; ">Please enter a valid phone number.</label>
</p>
</fieldset>
<fieldset class="w50">
<p class="form-subscr-field rowElem right">
<label for="contact_zip" id="contact_zip_label">Zip Code: *</label>
<input id="contact_zip" type="text" name="contact_zip" class="inputbox" size="10" required minlength="5"/>
<label class="error error-tip" for="contact_zip" id="zip_error" style="display: none; ">Please enter your shipping zip code.</label>
</p>
</fieldset>
<p class="form-subscr-field">
<label for="contact_best_time" id="contact_best_time_label">Best time to Contact:</label>
<input id="contact_best_time" type="text" name="contact_best_time" class="inputbox" size="10" />
</p>
<p class="form-subscr-field">
<label for="contact_message" id="comment_label">Your message: *</label>
<textarea id="contact_message" name="contact_message" rows="8" cols="30" required minlength="2"></textarea>
<label class="error error-tip" for="contact_message" id="message_error" style="display: none; ">This field is required.</label>
</p>
<input type="hidden" id="timestamp" name="timestamp" value="<?php
$hourdiff = "2"; // hours diff btwn server and local time
$insertdate = date("l, d F Y h:i a",time() + ($hourdiff * 3600));
print ("$insertdate");
?>" />
<div class="submit-contact">
<p>
<input type="submit" name="submit" class="cool-button csbutton-color spot-action" id="contact_button" value="Submit" />
</p>
</div>
</form>
그냥 호기심; 서버에서도 유효성을 검사 할 수 있습니까? –
즉시 확인하는 문제는 아직 값이없는 필드에 대해 오류를 표시한다는 것입니다. 수동으로 어느 것이 추악 할 지, 아니면 일종의 플러그인/라이브러리 (당신이 직접 작성할 수있는)로 이것을 깨뜨려야합니다. –
@ 제필 : 귀하의 질문을 이해할 수 있도록 서버에서 유효성 검사가 무엇을 의미하는지 설명하십시오. – adamdehaven