標籤:style blog color io os ar sp div on
1.判斷文本是否為空白:
1 $(".commonVerify").blur(function(){2 if($.trim($(this).val())==‘‘){3 $("#tips").html("不可為空");4 $(this).focus();}5 });
$.trim()是去掉字串的前後括弧。
2.判斷電話號碼:
1 //判斷電話號碼是否正確 2 $(".mobil").blur(function(){ 3 if(!($.trim($(this).val())==‘‘)){ 4 if(!$(this).val().match(/^1[3|4|5|8][0-9]\d{4,8}$/)){ 5 $(this).focus(); 6 $("#tips").html("電話號碼錯誤"); 7 }else{ 8 $("#tips").html(""); 9 }10 }else{11 $(this).focus();12 $("#tips").html("不可為空");13 }14 });15
3.判斷郵遞區號是否正確:
1 //判斷郵遞區號是否正確 2 $("#postalCode").blur(function(){ 3 if(!($.trim($(this).val())==‘‘)){ 4 if(!$(this).val().match(/^[1-9]\d{5}$/)){ 5 $(this).focus(); 6 $("#tips").html("郵遞區號錯誤:應為6位元"); 7 }else{ 8 $("#tips").html(""); 9 }10 } else{11 $(this).focus();12 $("#tips").html("不可為空");13 }14 });
4.判斷身份證是否正確:
1 $("#idCard").blur(function() { 2 var idCard = $("#idCard").val(); 3 var bo = /^(\d{6})(18|19|20)?(\d{2})([01]\d)([0123]\d)(\d{3})(\d|X)?$/.test(idCard); 4 var year = idCard.substr(6, 4); 5 var month = idCard.substr(10, 2); 6 var day = idCard.substr(12, 2); 7 8 if(!($.trim($(this).val())==‘‘)){ 9 if (bo == false || month > 12 || day > 31) {10 11 $("#tips").html("請輸入正確的社會安全號碼碼!");12 }else{13 $("#tips").html("");14 }15 }else{16 $(this).focus();17 $("#tips").html("不可為空");18 }19 });
5.判斷銀行卡號:
1 $("#bankCard").blur(function(){ 2 if(!($.trim($(this).val())==‘‘)){ 3 if(!$(this).val().match(/^\d{19}$/)){ 4 $(this).focus(); 5 $("#tips").html("格式錯誤,應該為19位元字"); 6 }else{ 7 $("#tips").html(""); 8 } 9 } else{10 $(this).focus();11 $("#tips").html("不可為空");12 }13 14 })
jQuery 表單驗證