【JavaScript】手機號碼驗證

<script type='text/javascript'>
 function IsNumeric(sText)
 {     //判斷是否為數值
       var ValidChars = "0123456789";
      參考本Blog:判斷是否為數字--IsNumeric
 }    

 function Left(str, n)
 {

    參考本Blog:字串擷取--Left
 }

function ChkMobile()
{

if (document.form1.p_mobile1.value.length!=0 && document.form1.p_mobile2.value.length!=0)
    {
        var handy=document.form1.p_mobile1.value.length+document.form1.p_mobile2.value.length;

             if (IsNumeric(document.form1.p_mobile1.value)==false)
            {
                   alert('手機號碼只能為數字!');
             document.form1.p_mobile1.focus();
             return false;
             }
             if (IsNumeric(document.form1.p_mobile2.value)==false)
            {
                   alert('手機號碼只能為數字!');
             document.form1.p_mobile2.focus();
             return false;
             }
             if (handy!=10)
            {
                   alert('手機號碼只能為10碼!');
             document.form1.p_mobile2.focus();
             return false;
             }
             if (Left(document.form1.p_mobile1.value,2)!='09')
            {
                  
                   alert('手機號碼只能為09開頭的數字!');
             document.form1.p_mobile1.focus();
             return false;
             }
    }
}
</script>

部份HTML碼:

<html>
<body>
<form name="form1" method="post" action="">
手機號碼 :
  <input name="p_mobile1" type="text" width="15" maxlength="4">
  -
<input name="p_mobile2" type="text" width="20" maxlength="6">
<input name="OK" type="button" onClick="ChkMobile();" value="OK">
</form>
</body>
</html>

留言

這個網誌中的熱門文章

【ASP】日期轉換(西元<-->民國)

【VB】使用NPOI元件來匯出Excel--DataTableToExcel

【SQL】符號切割字串變成多欄