0

I am trying to check a search box and this is my query which works but has a couple of problems.

 // =================================
 var check = new Regex("[^A-Za-z]");
 Match m = check.Match(searchQuery);
 if (m.Equals(check) != true)
 {
     pnl_Message.Visible = true;
     pnl_Message.CssClass = "messageTable";
     lbl_message.Text = " * Only Letters are allowed";
  }

The If statement Equals says Suspicious comparison !
but works!
the thing is it always goes in the if statement no matter what I set the expression too.

How should I be Querying the searchQuery?

2

2 Answers 2

1

Your Equals is comparing a Match to a Regex - they're different classes, so it's never true, and you always go into your if block.

You probably want if (m.Success) - that checks whether the value returned by check.Match(...) was a successful match (i.e. the input contained a non-letter) or not.

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

Comments

0

You are using Object's Equals method which is the base class for Regex

Use IsMatch method of Regex class i.e check.IsMatch(searchQuery)

Comments

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.