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?

शेखर
  • 17,412
  • 13
  • 61
  • 117
StudentRik
  • 1,049
  • 2
  • 20
  • 37

2 Answers2

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.

Rawling
  • 49,248
  • 7
  • 89
  • 127
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)

Anirudha
  • 32,393
  • 7
  • 68
  • 89