我有这个验证电话号码的代码:
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>
这个功能是对的还是我做错了什么?
答案 0 :(得分: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})/
答案 1 :(得分:0)
查看非常通用的电话号码 - 传真号验证码 - 希望它有用 http://oracleadf-java.blogspot.in/2013/05/phone-number-custom-validation-in-adf.html
public void phoneNoValidator (FacesContext facesContext, UIComponent uIComponent, 对象对象){
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"));
}
}
}