Excluding apostrophes in regular expression?

I am trying to validate a form using the regex found here http://regexlib.com/ . What I'm trying to do is filter out all the characters except az, commas and apostrophes. If I use this code:

<cfinput name="FirstName" type="text" class="fieldwidth" maxlength="90" required="yes" validateat="onsubmit,onserver" message="Please ensure you give your First Name and it does not contain any special characters except hyphens or apostrophes." validate="regular_expression" pattern="^([a-zA-Z'-]+)$" /> 

I get the following error: Unmatched [] in the expression. I realized that this applies to the apostrophe because it works if I use this code (but don't allow apostrophes):

 <cfinput name="FirstName" type="text" class="fieldwidth" maxlength="90" required="yes" validateat="onsubmit,onserver" message="Please ensure you give your First Name and it does not contain any special characters except hyphens or apostrophes." validate="regular_expression" pattern="^([a-zA-Z-]+)$" /> 

So, I am wondering if there is a special way to avoid apostrophes when using regular expressions?

EDIT

I think I found where the problem arises (thanks xanatos), I don’t know how to fix it. Basically, CF generates a hidden field to validate the field as follows:

 <input type='hidden' name='FirstName_CFFORMREGEX' value='^([a-zA-Z'-]+)$'> 

Since it uses single apostrophes rather than speech marks around the value, it interprets the apostrophe as the end of the value.

+7
source share
2 answers

I think there is an error in the implementation of cfinput. It probably uses the string you pass in the template in Javascript Regex, but it uses ' to quote it. Therefore, it converts it to:

 new Regex('^([a-zA-Z'-]+)$') 

Try replacing the quote with \x27 (this is the code for a separate quote)

+7
source

Unbeatable] is that a hyphen is treated as a range between two characters around it. Put a hyphen at the beginning as a best practice.

 ^([-a-zA-Z']+)$ 
0
source