Skip to content

Instantly share code, notes, and snippets.

@RhinoLu
Last active March 9, 2016 08:55
Show Gist options
  • Save RhinoLu/1caf68ffd578eb0ef0de to your computer and use it in GitHub Desktop.
Save RhinoLu/1caf68ffd578eb0ef0de to your computer and use it in GitHub Desktop.
form validation 表單驗證
checkForm(): boolean {
var msg: string = "";
if ($("#form_name").val().length < 1) {
msg += "請輸入中文姓名\n";
}else{
var chineseCount: number = $("#form_name").val().split(/[\u4e00-\u9fa5]/).length - 1;
trace("chineseCount : " + chineseCount);
trace("姓名.length : " + $("#form_name").val().length);
if($("#form_name").val().length != chineseCount){
msg += "姓名請輸入中文字\n";
}
}
var tmp_mobile: string = $("#form_tel").val();
//trace("[" + tmp_mobile.slice(0,2) + "]");
if (tmp_mobile.length != 10 || tmp_mobile.slice(0, 2) != "09") {
msg += "手機號碼格式錯誤\n";
}
if (!/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test($("#form_email").val())) {
msg += "Email 格式錯誤\n";
}
if ($("#form_vcode").val().length < 1) {
msg += "請輸入驗證碼\n";
}
if (!$("#form_btn_check").hasClass("checked")) {
msg += "請確定以上為正確之個人資料,並同意提供給本行銷活動使用\n";
}
if (msg.length < 1) {
return true;
} else {
alert(msg);
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment