Regular Expression for Textbox


Badge +3

I have a Single Line Textbox that is used for Phone numbers to populate our Active Directory. The format that we use for AD is XXX.XXX.XXXX

 

 

The regular Expression looks like: (d{3}).(d{3}).(d{4})

 

problem that I am having is that it will accept any other input such as XXX-XXX-XXXX

 

How do I force it to be separated by periods.


5 replies

Userlevel 6
Badge +16

(d{3})[.](d{3})[.](d{4})

Userlevel 6
Badge +16

Tim Edwards​, have you tested it?

Badge +6

The problem is that regular expressions use the period as substitute for any character at all and to explain that you mean the period itself, you must escape it with a backslash: (d{3}).(d{3}).(d{4})

Badge +3

Thank you both your comments,

Fernando I have tested your solution so far it is working.

Alexey I will try yours today and see what the difference is.

Badge +6

There should be no difference, either regex should give you the same result. I only added my response to explain why.

What Fernando suggested was to "escape" the period by enclosing it in a "limited subset of acceptable characters" construct ( [ . ] ) where you could specify one or more acceptable delimiters like dashes (which you'd need to escape with a backslash inside the square brackets, but not outside): [—.–-], etc.

Reply