0

I am trying to validate string should not start with some character and should not contain <>.

[Required]
[Display(Name = "First name")]
[MaxLength(50)]
[RegularExpression(@"(?=(^(?!.*[><]).*$))(?=(^(?![@\+\-=\*]).*))", ErrorMessage = "firstname"+ Constants.DisplayMessage)]
public string firstname { get; set; }

this regex is working in javascript. but it is not working in c#. I have already spent almost 1 hours on it but no result please help me. ya also tried using (^(?!.*[><]).*$)|(^(?![@\+\-=\*]).*) this regex.but it is not working. I am not good at regex so please help me.

praval
  • 206
  • 1
  • 3
  • 14
  • 1
    What is this regex for? What do you expect from _$_ in the middle of expression? – Alex Seleznyov Jan 19 '18 at 06:10
  • Please give some background for what this regex is supposed to be doing. It may even be that you could use a simpler pattern in both places. – Tim Biegeleisen Jan 19 '18 at 06:11
  • @AlexSeleznyov you can use https://regexr.com/ it will help you to understand what I am trying to do. – praval Jan 19 '18 at 06:12
  • @TimBiegeleisen cannot contain < or > and cannot start with @, -, =, * characters – praval Jan 19 '18 at 06:12
  • https://stackoverflow.com/questions/48295781/regex-is-not-working-in-regularexpression-attribute-on-c-sharp Please check this answer once, it may help. – JyothiJ Jan 19 '18 at 06:38

2 Answers2

2

Based on your description what the regex needs to do, the following pattern should work:

^(?![@=*-])(?!.*[<>]).*$

Explanation:

^
(?![@=*-])    from the start of string, assert that @=*- is not the first character
(?!.*[<>])    also assert that <> does not appear anywhere in the string
.*            then match anything (our assertions having be proven true)
$

Demo

This pattern is also working for C#, as you can see by exploring the second demo below.

C# Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • thank you, I am testing it on my c# code give me 5 min. – praval Jan 19 '18 at 06:19
  • it is working on javascript but not working on c#. and you are testing it on javascript regex engine. – praval Jan 19 '18 at 06:25
  • in c# I am using RegularExpression Attribute. – praval Jan 19 '18 at 06:26
  • @pravaljain The pattern seems to work fine for me in C#. Have a look at the demo I added to my answer. My guess is that you are using one of their APIs incorrectly. – Tim Biegeleisen Jan 19 '18 at 06:30
  • thanks, Tim but for your demo, I replaced your regex with mine and my is also working but I don't know why it is not working on the attribute. I think I am using API incorrectly. – praval Jan 19 '18 at 06:34
  • @pravaljain Maybe you could ask a specific C# question with a working pattern and ask why your code isn't working. – Tim Biegeleisen Jan 19 '18 at 06:38
0

Having proper set validator, it works in C# too public class NameProp { string m_firstname;

    [Required]
    [Display(Name = "First name")]
    [MaxLength(50)]
    [RegularExpression(@"^(?![@=*-])(?!.*[<>]).*$", ErrorMessage = "firstname contains <> or start with @=*-")]
    public string firstname
    {
        get { return m_firstname; }

        set
        {
            Validator.ValidateProperty(value,
                new ValidationContext(this, null, null) { MemberName = "firstname" });
            m_firstname = value;
        }
    }

}

public class Program {

    static void Main(string[] args)
    {
        NameProp np = new NameProp();
        try
        {
            np.firstname = "<>JJ";
        }
        catch (ValidationException ex)
        {
            Console.WriteLine(ex.Message);
        }

    }
}
JyothiJ
  • 315
  • 1
  • 11
  • Also please refer https://www.codeproject.com/Tips/550784/Validation-using-DataAnnotations for more details – JyothiJ Jan 19 '18 at 07:07