如何将此格式更改为02X-XXXXXXX?

时间:2014-10-19 04:25:30

标签: javascript

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的话),我想更改它以使其显示并发出警报是数字) 任何人都可以帮我这个吗?

1 个答案:

答案 0 :(得分: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;
}