Tuesday, July 21, 2015

ColdFusion email validation regex timeout

Once upon a time, before ColdFusion 9 introduced isvalid(), I made my own function using regex I found online for validating emails.
([A-Za-z0-9]|.|_|\-)+@([A-Za-z0-9]|_|\-)+[.]{1}([A-Za-z]|.|_|\-)+
Validation worked fine. Sometimes the form was slow, for seemingly no reason with there being no queries or anything which would slow things down. As it was occasional and seemingly unreplicatable, I ignored it.
Yesterday, a particular form, which was prepopped with a new member's email, stopped working. After submit, and a few minutes waiting, there was a timeout error with error pointing at cfmail. Commenting out cfmail just moved the line reported to a cfoutput tag. After some collaborative troubleshooting, we figured out that the issue was the long email.
I replace my custom function with isvalid(), which fixed the problem. However, the timeout still made no sense, so I took a close look at the regex I was using. The issue was that the + was outside the parenthesis, instead of inside. The longer the email, the longer ColdFusion would spend capturing all the possible matches. Adding ?: did not help. Moving the + inside, did. This is the correct regex to use if not using isvalid().
([A-Za-z0-9\._\-]+)@([A-Za-z0-9_\-]+)[.]{1}([A-Za-z\._\-]+)

No comments:

Post a Comment