<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/jquery.validate.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-validation@1.19.5/dist/localization/messages_zh_TW.min.js"></script>
<style>
.error{
color:red;
}
</style>
<script>
$(function() {
$("#myForm").validate({
submitHandler: function(form) { //驗證成功執行
$('#submit').attr('disabled', true); //關閉送出
setIdAttribute('main-content', 'd-none'); //關閉主畫面
setIdAttribute('show', 'my-5'); //開啟loading畫面
insertData(); //寫入函式
document.getElementById('myForm').reset(); //表單重置
return false;
},
rules: { //規則
"title": { //表單 name
"required": true, //必填
"email": true, //email
"url": true, //網址
"number": true //數字
}
},
messages: { //訊息
"title": { //表單 name
"required": "必填", //必填
"email": "Email格式不正確", //email
"url": "網址格式不正確", //網址
"number": "數字格式不正確" //數字
}
}
});
});
/*===================================
寫入函式
===================================*/
function insertData() {
alert('寫入函式');
}
/*===============================
指定 id -> class
setIdAttribute('main-content',''); //開啟主畫面
setIdAttribute('show','d-none'); //關閉等待畫面
setIdAttribute('main-content','d-none');//關閉主畫面
setIdAttribute('show','my-5'); //開啟等待畫面
===============================*/
function setIdAttribute(id, elementClass){
document.getElementById(id).setAttribute("class", elementClass);
}
</script>