Showing posts with label regex. Show all posts
Showing posts with label regex. Show all posts

Friday, September 12, 2008

RegEx broke my phone

We had a bug turned in on a form that was not accepting valid phone numbers, or so it seemed. The number used was something like 233-122-2323. Looks like a valid phone number right? I dug into the form field and found that it was a cfinput tag utilizing the validate="telephone". We recently moved to CF8 and I was wondering if this was a CF8 issue that was somehow unique. I looked at the generated source in order to evaluate the resulting js that is used to validate the form on submit. That led me to this code:

//form element phone 'TELEPHONE' validation checks
if (!_CF_checkphone(_CF_this['phone'].value, true))
{
alert(_CF_this['phone'].value)
_CF_onError(_CF_this, "phone", _CF_this['phone'].value, "A valid phone number is required.");
_CF_error_exists = true;
}
We can find the code for _CF_checkphone buried in this file, depending on your instance:
\JRUN4\servers\[instance]\cfusion-ear\cfusion-war\CFIDE\scripts\cfform.js
Finding the function in this file revealed the regular expression that is being used to validate phone number.

/^(((1))?[,\-,\.]?([\\(]?([1-9][0-9]{2})[\\)]?))?[,\-,\.]?([^0-1]){1}([0-9]){2}[ ,\-,\.]?([0-9]){4}(()((x){0,1}([0-9]){1,5}){0,1})?$/

1-800-322-5544 or 220-122-2323 (the number used on the form)
Now I'm not a regex guru nor do I work with it every day, so color coding is a definite help for me. There are some nice tools out there that can help you test regex both pay and free. I've been playing with RegExBuilder (free) lately. It doesn't have all the bells and whistles like being able to switch the regex engine, but it works for what I need right now. Anywho, let's break this down:

**- the ? makes the 1 optional

**- this has to be a digit between 1 and 9, never 0

**- two digits that are between 0 and 9

**- any character that is not 0 or 1. Surprisingly, this allows non digits

**- two digits that are between 0 and 9

**- four digits that are between 0 and 9

So according to the test number that was entered in the test, it violates rule **.
The funny thing is that we could have entered anything else besides 0 or 1. I tested 701-B86-5566 and it worked. But anywho… I think this form is performing as expected, unless of course there are valid phone numbers with the ** being 0 or 1. I wouldn’t know where to find that info and a brief google session didn’t turn anything up. I gave up quickly because didn't feel like digging into that right now. I'll leave that up to a more ambitious person. But the question to be answered is whether or not this should be reported as a bug and request Adobe to fix that regex in CF8 to be more accurate on the [^0-1] test. Why not [2-9]?

Blessings...