Skip to content

Instantly share code, notes, and snippets.

@annaliahsiao
Created December 13, 2018 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save annaliahsiao/73c0bca5e1ece75271d96ddfcd408900 to your computer and use it in GitHub Desktop.
Save annaliahsiao/73c0bca5e1ece75271d96ddfcd408900 to your computer and use it in GitHub Desktop.
簡單的表單驗證
var re_nm = /^\W$/,
re_idt = /^[A-Z]{1}\d{9}$/,
re_tel = /^0\d{1}\d{6,8}$/,
re_ctel = /^09\d{8}$/,
re_eml = /^\S+@\w+((\.|-)\w+)*\.\w+$/,
errstr = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_.#@()*[]{}|!~';:<>,?/\&^%$\"+-";
function form_chk(){
var name = $('input[name="name"]').val(),
identity = $('input[name="identity"]').val(),
tel = $('input[name="tel"]').val(),
ctel = $('input[name="ctel"]').val(),
email = $('input[name="email"]').val(),
errtex = '',
errck = '',
chk = true;
if(name == ''){
errtex +='請輸入您的姓名!\n';
chk = false;
};
for(var i=0 ; i<name.length ;i++){
errck = errstr.indexOf(name.charAt(i))
if( errck > 0 ){
errtex +='您的姓名輸入格式錯誤!\n';
chk = false;
break;
};
};
if(identity == ''){
errtex +='請輸入您的身分證號碼!\n';
chk = false;
};
if(identity != ''){
if(!re_idt.test(identity)){
errtex +='您的身分證格式輸入錯誤!\n';
chk = false;
};
}
if(tel == ''){
errtex +='請輸入您的電話號碼!\n';
chk = false;
}else if(!re_tel.test(tel)){
errtex +='您的電話號碼輸入錯誤!\n';
chk = false;
};
if(ctel == ''){
errtex +='請輸入您的手機號碼!\n';
chk = false;
}else if(!re_ctel.test(ctel)){
errtex +='您的手機號碼輸入錯誤!\n';
chk = false;
};
if(email == ''){
errtex +='請輸入您的信箱!\n';
chk = false;
}else if(!re_eml.test(email)){
errtex +='您的信箱輸入錯誤!\n';
chk = false;
};
if( chk === true){
alert('form sent!');
}else{
alert(errtex);
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment