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.

5
  • 1
    What is this regex for? What do you expect from $ in the middle of expression? Commented Jan 19, 2018 at 6: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. Commented Jan 19, 2018 at 6:11
  • @AlexSeleznyov you can use regexr.com it will help you to understand what I am trying to do. Commented Jan 19, 2018 at 6:12
  • @TimBiegeleisen cannot contain < or > and cannot start with @, -, =, * characters Commented Jan 19, 2018 at 6:12
  • stackoverflow.com/questions/48295781/… Please check this answer once, it may help. Commented Jan 19, 2018 at 6:38

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

6 Comments

thank you, I am testing it on my c# code give me 5 min.
it is working on javascript but not working on c#. and you are testing it on javascript regex engine.
in c# I am using RegularExpression Attribute.
@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.
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.
|
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);
        }

    }
}

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.