Skip to content

Instantly share code, notes, and snippets.

@rc1021
Created September 23, 2015 18:21
Show Gist options
  • Save rc1021/7fb68065c220a9530a0c to your computer and use it in GitHub Desktop.
Save rc1021/7fb68065c220a9530a0c to your computer and use it in GitHub Desktop.
<script type="text/javascript">
$(function () {
$("#user_success").on('submit', function(){
var message = "";
var emptyValue = false;
var name = $('[name="name"]', $(this)).val();
var area = $('select[name="area"] option:selected', $(this)).val();
var time = $('select[name="time"] option:selected', $(this)).val();
var email = $('[name="email"]', $(this)).val();
var phone = $('[name="phone"]', $(this)).val();
if (area=='blank') {
alert('請選擇區域');
return false;
};
if (time=='blank') {
alert('請選擇時段');
return false;
};
if(name == "")
{
message = "請輸入姓名\n";
emptyValue = true;
}
if(email == "")
{
message = message + "請輸入email\n";
emptyValue = true;
}
else if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))
{
message = message + "email輸入格式錯誤\n";
emptyValue = true;
}
if(phone == "")
{
message = message + "請輸入電話\n";
emptyValue = true;
}
else if(!/^[09]{2}[0-9]{8}$/.test(phone))
{
message = message + "手機號碼格式錯誤\n";
emptyValue = true;
}
if(emptyValue)
{
alert(message);
return false;
}
else
{
var post_url = $(this).attr('action');
$.ajax({
type: "POST",
cache: false,
url: post_url, // preview.php
data: $("#user_success").serializeArray(), // all form fields
success: function (data) {
alert(data);
}
});
document.getElementById("user_success").reset();
}
return false;
});
});
</script>
@rc1021
Copy link
Author

rc1021 commented Sep 23, 2015

line: 55 ~ 65
就是為何送出表單不換頁的原因

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment