Search code examples
c#.netregexicq

RegEx for ICQ UIN


What RegularExpressionValidator.ValidationExpression should I use to allow only ICQ UIN like input?

xxx-xxx-xxx and xxx-xxx-xx and xx-xxx-xxx and xxxxxxxxx so on..

i.e. with dash as separator and without.


Solution

  • You can use the following simple expression.

    ^([0-9]-?){7,8}[0-9]$
    

    The drawback is, that it allows things like 1-2-3-4-5-6-7-8. If you want to restrict the layout more, you can use complexer expressions.

    ^(?=([0-9]-?){8,9})([0-9]{2,3}-?)*(?<!-)$
    

    This expression asserts that the string contains exactly eight or nine numbers and some dashes using the positive look ahead assertion (?=([0-9]-?){8}). Then it matches groups of two or three numbers optionaly separated by dashes and finally asserts that the string does not end with a dash using the negative look behind assertion (?<!-).

    This still allows some irregular patterns like 12-34567-89. If you want to eliminate them, too, you will have to list all allowed patterns. But I suggest not to do so, but allow as much flexibility as possible - I would allow every string with eight or nine numbers and any number of dashes - even --123---4-5-67--8 and then reformt the user input in a predefined format.

    ^(-*[0-9]-*){8,9}$