2014-10-19 68 views
-2
function checkcomplex(whichcontrol) { 
    var passcheck=true; 
    var x=0; 
    if (whichcontrol.value.length==0) 
     return true; 
    else if (whichcontrol.value.length!=11) 
     passcheck=false; 
    else { 
     while (passcheck && x<3) { 
    if (whichcontrol.value.charAt(x) >='0' && whichcontrol.value.charAt(x) <='9')x++; 
    else 
     passcheck=false; 
     } 
    if (whichcontrol.value.charAt(x)=='-') x++; 
    else 
     passcheck=false; 
     while (passcheck && x<6) { 
    if (whichcontrol.value.charAt(x) >='0' && whichcontrol.value.charAt(x) <='9')x++; 
    else 
     passcheck=false; 
     } 
     if (whichcontrol.value.charAt(x)=='-') x++; 
    else 
     passcheck=false; 
     while (passcheck && x<11) { 
    if (whichcontrol.value.charAt(x) >='0' && whichcontrol.value.charAt(x) <='9')x++; 
    else 
     passcheck=false; 
     } 
     } 
     //end else 
    if (passcheck) 
     return true; 
    else 
     return errorinfield(whichcontrol,"Must be of the form 999-99-9999!"); 
    } 

如果這個號碼的格式不是999-99-9999,並且我想改變這個號碼以使其出現並提醒,如果號碼不是02X-XXXXXXX (X代表數字) 任何人都可以幫助我嗎?如何將此格式更改爲02X-XXXXXXX之類的內容?

回答

0

像這樣的東西?

function checkcomplex(whichcontrol) { 
    // You can make a trim if you like 
    whichcontrol.value = whichcontrol.value.trim(); 
    // Check length 
    if(whichcontrol.value.length == 0) 
     return true; 
    // Check if value do not match pattern 
    //   Must start with "02" + one digit + "-" + 7 digits and end of string 
    if(!whichcontrol.value.match(/^02\d-\d\d\d\d\d\d\d$/)) 
     return errorinfield(whichcontrol,"Must be of the form 02X-XXXXXXX!"); 
    // It is valid 
    return true; 
} 
+0

嘿,你可能想了解使用正則表達式設置字符限制。 '\ d {3}'相當於'\ d \ d \ d'。 :) – 2014-10-19 06:08:38

+0

我知道字符限制。我只是想簡化@blackiechan。 – GramThanos 2014-10-19 06:12:15

相關問題