-
1. Re: Phone field validation
Steve L Walker Oct 20, 2010 12:21 AM (in response to Bibhu Bikash Nayak)The script below is attached to the exit event of a Text Field with a maximum length of 14. The script enables the entry of 123 456 7890, also.
// form1.page1.subform1.telephone::exit - (JavaScript, client)
if (!(this.isNull)) {
var str = this.rawValue;
var regExp = /^\d{10}$/;
if (regExp.test(str)) {
this.rawValue = "(" + str.substr(0,3) + ") " + str.substr(3,3) + "-" + str.substr(6,4);
}
else {
regExp = /^[1-9]\d{2}\s\d{3}\s\d{4}$/;
if (regExp.test(str)) {
this.rawValue = "(" + str.substr(0,3) + ") " + str.substr(4,3) + "-" + str.substr(8,4);
}
else {
regExp = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
if (!(regExp.test(str))) {
xfa.host.messageBox("Please enter the telephone number in the format '(999) 999-9999'.");
this.rawValue = null;
}
}
}
}
The script allows the entry of
1234567890 with the result (123) 456-7890
123 456 7890 with the result (123) 456-7890
(123)456-7890 with the result (123)456-7890
(123) 456-7890 with the result (123) 456-7890
otherwise the result is null.
Steve
-
2. Re: Phone field validation
Bibhu Bikash Nayak Oct 20, 2010 1:08 AM (in response to Steve L Walker)Hello Steve,
Thanks a lot. That worked perfectly. I want to set the focus to the field if the result is null. Could I just add a set focus script to it ?
Thanks.
Bibhu.
-
3. Re: Phone field validation
Steve L Walker Oct 20, 2010 6:59 AM (in response to Bibhu Bikash Nayak)// form1.page1.subform1.telephone::exit - (JavaScript, client)
if (!(this.isNull)) {
var str = this.rawValue;
var regExp = /^\d{10}$/;
if (regExp.test(str)) {
this.rawValue = "(" + str.substr(0,3) + ") " + str.substr(3,3) + "-" + str.substr(6,4);
}
else {
regExp = /^[1-9]\d{2}\s\d{3}\s\d{4}$/;
if (regExp.test(str)) {
this.rawValue = "(" + str.substr(0,3) + ") " + str.substr(4,3) + "-" + str.substr(8,4);
}
else {
regExp = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;
if (!(regExp.test(str))) {
xfa.host.messageBox("Please enter the telephone number in the format '(999) 999-9999'.");
this.rawValue = null;
xfa.host.setFocus(this);
}
}
}
}
-
4. Re: Phone field validation
Bibhu Bikash Nayak Oct 20, 2010 9:41 PM (in response to Steve L Walker)Hello Steve,
Thanks for the solution.
Bibhu.
-
5. Re: Phone field validation
elmonocochino Sep 18, 2012 6:04 AM (in response to Steve L Walker)Sorry to re-open an old thread, but I thought this would be a good place to post this question:
How would I include additional entries into this script to convert to the same end result? Specifically 123-456-7890 and 123.456.7890.
I've tried inserting another if statement with /^\d{3}\-\d{3}\-\d{4}$/; as the variable, but i always seem to break something else.
I'm pretty new at this and have been trying for quite a few hours.



