I am applying regex on my window form using c#. but regular expression on field is not working
if (txtRefno.Text == "" && !Regex.IsMatch(txtRefno.Text, @"^[0-9]+$"))
{
msg.Text = "reference no. contain Invalid characters";
}
but its not working and accept everything.
I changed the pattern to "^[0-9]+$" but same result I receive; then I changed the condition of regex.IsMatch(inputstring, pattern) == false; then error should be displayed, but it also doesn’t work for me.
I can’t help you with the Regex epression as I’m not very strong on that but I think you should replace the AND with an OR in your if statement to allow the compiler to check the regex in the case the textbox is not empty.
or if you don’t want show the message in the case the texbox is empty you can use the below