Saturday, February 5, 2011

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.

  • 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}$
    
    Massa : Some comments: (1) when I used ICQ, I had a 6-digit number; (2) I think ICQ numbers cannot start with 0; (3) Just ignore the dashes is a nice policy IMHO: `^-*[1-9][0-9-]*$` would be enough...
    Nathan Fellman : Massa, you're giving away your internet age with this... My ICQ also has a 6-digit number, and I joined 11 or 12 years ago.

0 comments:

Post a Comment