2

我有这个验证电话号码的代码:

function phvalid() {
    var regexObj = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
    if (regexObj.test(subjectString)) {
        var formattedPhoneNumber = subjectString.replace(regexObj, "($1) $2-$3");
    } else {
        alert("Invalid Number");
    }
}

我正在尝试将其验证到以下 html 代码的正文中:

<p class="normal">Phone:
    <input type='text' id='ph' />
    <input type='button' onclick="phvalid();" value="Click" />
</p>

这个功能是对的还是我做错了什么?

4

2 回答 2

4

你从不定义subjectString

试试这个:http: //jsfiddle.net/bfvXL/1/

function phvalid() {

    var regexObj = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
    var subjectString = document.getElementById("ph").value;
    if (regexObj.test(subjectString))

    {
        var formattedPhoneNumber = subjectString.replace(regexObj, "($1) $2-$3");
    } else {
        alert("Invalid Number");
    }

}

这将运行您的功能,但我不确定您对该formattedPhoneNumber块的意图是什么。

此外,您需要确保您的 onclick 可以访问它。所以你必须把你的 js 放在你的 body 中,或者放在 dom 加载后运行的块中。

编辑:我相信这就是你想要的:http formattedPhoneNumber: //jsfiddle.net/bfvXL/2/

Edit2:对于下面评论中的新要求......试试这个:http: //jsfiddle.net/bfvXL/3/

function phvalid() {
    var subjectString = document.getElementById("ph").value;
    subjectString = subjectString.replace(/[^\d]/g, "");
    if (subjectString.length == 10) {
        var regexObj = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
        var formattedPhoneNumber = subjectString.replace(regexObj, "($1) $2-$3");
        document.getElementById("ph").value = formattedPhoneNumber;
    } else {
        alert("Invalid Number");
    }
}

实际上,由于regexObj在这种情况下没有用于验证,您可以简单地将其作为/(\d{3})(\d{3})(\d{4})/

于 2013-05-03T00:54:06.593 回答
0

看到非常通用的电话号码 - 传真号码验证码 - 希望它会有用 http://oracleadf-java.blogspot.in/2013/05/phone-number-custom-validation-in-adf.html

public void phoneNoValidator (FacesContext facesContext, UIComponent uIComponent, Object object) {

    String msg2 = "";
    if (object != null) {
        String phnNo = object.toString();
        int openB = 0;
        int closeB = 0;
        boolean closeFg = false;
        char[] xx = phnNo.toCharArray();
        for (char c : xx) {
            if (c == '(') {
                openB = openB + 1;
            } else if (c == ')') {
                closeB = closeB + 1;
            }

            if (closeB > openB) {
                closeFg = true; //closed brackets will not be more than open brackets at any given time.
            }
        }
        //if openB=0 then no. of closing and opening brackets equal || opening bracket must always come before closing brackets
        //closing brackets must not come before first occurrence of openning bracket
        if (openB != closeB || closeFg == true || (phnNo.lastIndexOf("(") > phnNo.lastIndexOf(")")) ||
            (phnNo.indexOf(")") < phnNo.indexOf("("))) {
            msg2 = "Brackets not closed properly.";
            FacesMessage message2 = new FacesMessage(msg2);
            message2.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message2);
        }
        if (phnNo.contains("()")) {
            msg2 = "Empty Brackets are not allowed.";
            FacesMessage message2 = new FacesMessage(msg2);
            message2.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message2);
        }
        if (phnNo.contains("(.") || phnNo.contains("(-") || phnNo.contains("-)")) {
            msg2 = "Invalid Phone Number.Check content inside brackets.";
            FacesMessage message2 = new FacesMessage(msg2);
            message2.setSeverity(FacesMessage.SEVERITY_ERROR);
            throw new ValidatorException(message2);
        }

        openB = 0;
        closeB = 0;
        closeFg = false;
        //check for valid language name.Allowed- brackets,dots,hyphen

        String expression = "([0-9\\-\\+\\(\\)]+)";
        CharSequence inputStr = phnNo;
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);
        String error = "Invalid Phone Number";
        System.out.println("Index of plus is--->" + phnNo.lastIndexOf("+"));
        System.out.println("Bracket index--->" + phnNo.charAt(0));

        if (matcher.matches()) {
            if (phnNo.contains("++") || phnNo.contains("--")) {
                throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, error,
                                                              "Can not contain two hyphen(--) or plus(++)"));
            } else if (phnNo.lastIndexOf("+") > 1) {
                throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, error,
                                                              "Plus sign should be in proper place"));
            } else if (phnNo.lastIndexOf("+") == 1 && phnNo.charAt(0) != '(') {
                throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, error,
                                                              "Plus sign should be in proper place"));
            } else if (phnNo.startsWith(" ") || phnNo.endsWith(" ")) {
                throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, error,
                                                              "Space Not allowed at start and end"));
            }

        } else {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, error,
                                                          "Only numeric character,+,() and - allowed"));
        }

    }
}
于 2013-06-12T06:27:47.207 回答