0

I have to have two different Regex patterns for the same ID. Reason being I have different error message requirements for all three. Problem is that the last Regex interferes with the second's leading space validation. I have tried \s, ^\s, [ \t], (?! ) and a variety of other harebrained ideas that I came up with. Same as Above regex is conflicting with StreetAddress2 and allowing leading white space. No trim that I have tried has worked. Except when I comment out the SameAsAbove.

public const string StreetAddress = @"^(?!(SAME|SAME AS ABOVE|same|same as above))[a-zA-Z0-9]{1}[a-zA-Z0-9&-\/.# ]{0,49}";

public const string StreetAddress2 = @"^(?! )[^\s][a-zA-Z0-9]{1}[a-zA-Z0-9&-\/.# ]{0,49}"; 

public const string SameAsAbove = @"^(?!(SAME|SAME AS ABOVE|same|same as above|Same|Same as above|Same as Above)).*$";

The first two give the message:

public const string StreetAddress = "Street may contain only alphanumeric characters, ., #, /, -, & or white space";

The last one gives this message:

public const string SameAsAbove = "Same or Same As Above is not allowed";

<asp:RegularExpressionValidator 
 ID="RegExpDFSRegistrationAddress" 
 ControlToValidate="txtDFSRegistrationAddress"
 runat="server" 
 CssClass="Error" />

RegExpBusinessContactAddress.ValidationExpression = CommonRegularExpressions.StreetAddress2;

RegExpBusinessContactAddress.ErrorMessage = CommonErrorMessages.StreetAddress;

  • Are you trying to match a string that has no `same as above` substring? `@"(?si)^(?!.*SAME(?: AS ABOVE)?)"`? BTW, why are you using regex for this? In C#, you may use a [case insensitive *Contains*](https://stackoverflow.com/questions/17563929/how-to-make-string-contains-case-insensitive). – Wiktor Stribiżew Nov 20 '17 at 15:16
  • 1
    Add a language tag. Also, post some actual code. It is virtually impossible to figure out what is going wrong since you don't show how you use the regexes. – Mad Physicist Nov 20 '17 at 15:16
  • You should learn how to do case-insensitive matching for '`same(\s*as\s*above)?`' in your chosen (but unspecified) dialect of regexes. People are more inventive than your regexes are. And someone might write 'as above' without 'same'; would that matter? I'm still getting snail mail with 'whyisthismandatory' (in all caps, but I don't want to shout at you about it) as the second line of the address because some idiot made the second line of the address mandatory. – Jonathan Leffler Nov 20 '17 at 15:23
  • 1
    You should consider separate steps. One step would trim leading and trailing blanks (and normalize between word gaps to single blanks). Another step would reject the unwanted characters. And another step would reject unwanted verbiage. If the framework you're using doesn't make that possible, then maybe get a different/better (more flexible) framework? Or consult about how to achieve the required effect in the framework. – Jonathan Leffler Nov 20 '17 at 15:27
  • The separate steps is the StreetAddress2 and the SameAsAbove. That is the problem. The second step interferes with the first step. I am a beginning Regex user. – Teresa Scudder Nov 20 '17 at 15:51
  • Can you explain what the other error messages would be for when there is a match with one of the first two regular expressions? – Josh Withee Nov 21 '17 at 21:38
  • The only error messages are listed above, Marathon55. – Teresa Scudder Nov 30 '17 at 17:37

0 Answers0