2013-05-03 293 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")); 
     } 

    } 
}