Click here to Skip to main content
15,397,324 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
i have a textbox which is determining the time, on the form load i have written the code so that the time will be filled in the textbox which is in this format
09:09 AM
i am using the regex for validation purpose
so i have wirtten this code
C#
private bool time()
        {
            Regex regex = new Regex("^(1[0-2]|0[1-9]):[0-5][0-9]\040(AM|am|PM|pm)$");
            if (regex.IsMatch(textBox2.Text))
            {
                return true;
            }
            else
            {
                return false;
            }

       
       
        }

there is a button in the form which is for showing if the error is there then the error provider shows the error, and if not, then error provider doesnt blink, the code for button click is

the problem is when i keep the textbox blank and clicks the button, the error provider doesnt shows the error
even if i put ''09:09 st'' instead of ''09:09 pm'' it doesnt shows error
Posted
Updated 26-Dec-12 7:38am
v3
Comments
Suvabrata Roy 26-Dec-12 8:41am
   
Regex regex = new Regex("^(1[0-2]|0[1-9]):[0-5][0-9]\040(AM|PM|)$",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
sariqkhan 26-Dec-12 9:07am
   
even if i put 09:09 st instead of 09:09 am it works and there is no error provide which blinks
Sergey Alexandrovich Kryukov 26-Dec-12 16:59pm
   
I don't observe it. It shows the error even if this is "09:09 AM". Why \040? Why 1 and 2? The general idea is correct should work. Refine the detail.
And describe what exactly should match and what not. Do you mean it should be "hh:mm (AP|PM)" format? And remove different cases, just make the search not case-sensitive.
—SA

Please see my comment to the question. I found a bug: you forgot to escape ':'. Make it '\:', escaped.

Besides, I would greatly simplified it, employing additional validation via C# after Regex validation. Do this:

Use the pattern ^([0-9][0-9])\:([0-9][0-9]) (AM|PM)$, and make the case-insensitive match.

It will give you three string string groups, inside round brackets. Extract two groups with digits and test each for comparison with numbers:
C#
string digitGroupText = //... take from Regex
// it will be anything from 00 to 99, but only 00 to 59 is valid...

byte number = byte.Parse(digitGroupText); // as Regex validation is passed, this should always be successful

if (number > 59) //... then it is invalid


Got the idea? Regex along is not suitable for each and every validation.

Good luck,
—SA
   
v3
Comments
sariqkhan 27-Dec-12 1:57am
   
thank you sir
really thank you for replying and finding my problem
   
You are very welcome.
Good luck, call again.
—SA
Hi!,
Try to test for text blank first,in the time(), like this
C#
private bool time()
        {
            Regex regex = new Regex("^(1[0-2]|0[1-9]):[0-5][0-9]\040(AM|am|PM|pm)$");
            //Tests for Null, Empty or just white space.
            if (string.IsNullOrWhiteSpace(textBox2.Text))
            {
                return false;
            }
            if (regex.IsMatch(textBox2.Text))
            {
                return true;
            }
            else
            {
                return false;
            }
 
        }
   
Comments
sariqkhan 26-Dec-12 8:44am
   
sir your code is correct but the the problem is when i put
09:09 st instead of 09:09 PM it does not shows error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900