前提
- 这里就是简单的姓名电话地址和一个需要勾选的同意项
- tipShow是我自己写的调用一个提示弹窗的函数,可以换成自己的
代码
$('.inputPhone').on('input change', function () {
let phoneContent = $('.inputPhone').val(),
phoneLength = phoneContent.length;
if(phoneLength > 11) {
phoneContent = phoneContent.substr(0,11);
$('.inputPhone').val(phoneContent);
}
})
let name = '',
phone = '',
address = '',
agreeInput;
$('.Pinfo_btn_sure').on('click', function() {
name = $('.inputName').val();
phone = $('.inputPhone').val();
address = $('.inputAddress').val();
agreeInput = $('.Pinfo_main_checked input[type="checkbox"]:checked').length;
if (name == '') {
tipShow('请输入姓名');
} else if (phone == '' || !(/^1[3456789]\d{9}$/.test(phone))) {
tipShow('请输入正确手机号');
} else if (address == '') {
tipShow('请输入地址');
} else if (agreeInput != 1) {
tipShow('请勾选同意项');
} else {
console.log('留资成功');
}
})