asp.netregexreg-expressionvalidator

Regex RegularExpressionValidator


Sorry if this has already been answered but I could not find it here. I know its possible but have very limited knowledge of Regex. I have an ASP.NET project and I would like to check to see if the first two characters of a value starts with GY, BT, JE, and if so trigger a validation failure.

What I have at the moment inside my RegularExpressionValidator is:

/^(?i)[GY]{2}|^(?i)[BT]{2}|^(?i)[JE]{2}/

Any help would be greatly appreciated.

Thanks Jon


Solution

  • I am not sure if I understand correctly, but I guess your expression would be:

    /^(GY|BT|JE)/
    

    This would match all values starting with GY or BT or JE.

    If on the other way you would like to allow all strings that do not start with BY or BT or JE, this would be

    /^(?<!(GY|BT|JE)).*/