1. 程式人生 > >表單驗證 靠name獲取

表單驗證 靠name獲取

res 獲取 ems let nbsp jquer 手機 ear sub

表單 靠name獲取

<form class="add-form" name="form" action="#" method="post" autocomplete="on">
    <input type="text" name="user" placeholder="收貨人姓名"/><br/>
    <input type="text" name="tel" id="tel"  placeholder="手機號碼"/><br/>
    <input type="search" name="region" placeholder
="所在區域"/><br/> <!--<input type="text" name="tel" placeholder="詳細地址"/><br/>--> <textarea name="area" id="area" placeholder="詳細地址"></textarea> </form>

js

//判斷是否空
function beforeSubmit(form) {
    if (form.user.value == ‘‘) {
        alert(‘用戶名不能為空!‘);
        form.user.focus();
        
return false; } if (form.region.value == ‘‘) { alert(‘省市不能為空!‘); form.region.focus(); return false; } if (form.area.value == ‘‘) { //name獲取元素 name="region" // if ($("#area").value == ‘‘) { alert(‘地址不能為空!‘); $("#area").focus(); return
false; } } //jquery驗證手機號碼 function checkSubmitMobil() { if ($("#tel").val() == "") { alert("手機號碼不能為空!"); //$("#moileMsg").html("<font color=‘red‘>手機號碼不能為空!</font>"); $("#tel").focus(); return false; } // var pattern = /^1[34578]\d{9}$/; if (!$("#tel").val().match(/^1[34578]\d{9}$/)) { alert("手機號碼格式不正確!"); //$("#moileMsg").html("<font color=‘red‘>手機號碼格式不正確!請重新輸入!</font>"); $("#tel").focus(); return false; } return true; } $(function () { $(‘.submit‘).click(function () { if (checkSubmitMobil()&&beforeSubmit(form)) { $(‘form‘).submit(); } else { return false; } }); });

表單驗證 靠name獲取