2013-05-03 298 views
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> 

这是功能权限或者我做错了什么?

+1

您也可能考虑使用'input type ='tel'' http://www.w3.org/TR/html-markup/input.tel.html – 2013-05-03 00:51:12

+1

但是也要记住,如果你想让它在全球范围内工作,可能不会你可以做很多事情来验证。 – 2013-05-03 00:52:48

回答

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放在你的身体内,或者在dom被加载后运行的块中。

编辑:我相信这是你不想和formattedPhoneNumber什么:http://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})/

+0

谢谢!很棒!我如何将数值限制为10个数字而不是使用3-3-4? – user1618490 2013-05-03 01:08:12

+0

为什么?你想能够像'1sfalj2n3nio4567sdaf890jl'? – smerny 2013-05-03 01:15:19

+0

@ user1618490,如果这回答了您的问题,请将其标记为答案 – smerny 2013-08-21 20:45:51

0

看的很普通的电话号码,传真号码验证代码 - 希望这将是有用的 http://oracleadf-java.blogspot.in/2013/05/phone-number-custom-validation-in-adf.html

公共无效phoneNoValidator (FacesContext中的FacesContext, UIComponent UIComponent,而 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")); 
     } 

    } 
}