Click here to Skip to main content
15,916,683 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I have an text box which accepts hours. The text box accepts only numeric and decimal values.

am using the following regular expression

Regex expressionToValidate_String_Structure = new Regex("^\\d{1,2}([.]\\d{1})$");

So now how can i use negation in the above regular expression to check anything entered in the text box apart from the above expression?


Please provide me a solution in C#?
Posted
Comments
CodingLover 11-Mar-14 2:50am    
Which time format do you want to validate?
BillWoodruff 11-Mar-14 4:12am    
Define what you mean by "negation," and start writing code, and share the code here.
Matt T Heffron 13-Mar-14 15:18pm    
Why not just test for !expressionToValidate_String_Structure.IsMatch(input) ?
Often it's better to just code it the obvious way instead of trying to be clever. :-)

If you are using HTML5:

<input type="text" name="name" pattern="[0-9]" title="title" />

Otherwise you will need JavaScript.
 
Share this answer
 
See MSDN: Validate Textbox with Regular Expressions[^].
I.e. you may implement a leave text box event handler and trigger the check with whatever means there.
If you use Regex, you can ask the Success property. Negation is if you expect a pattern to result in match.Success == false.

Cheers
Andi
 
Share this answer
 
Regular expressions are the worst choice when it comes to validate dates and times (XML documents, also). But anyway, that is just a piece of advice.

Regarding your question, that does not make any sense, from a regular expression point of view.
You made it a complete line (symbols ^ & $ at start & end). So how do you expect anything more in it?

If there are some other elements that should be validated, then put them in your regular expression.
If not, then expression will not match, which is basically its purpose :)

I corrected it a little bit:
Original:
C#
Regex expressionToValidate_String_Structure = new Regex("^\\d{1,2}([.]\\d{1})$");

Modified
C#
Regex expressionToValidate_String_Structure = new Regex(@"^\d{1,2}(?:.\d{1})?$");

- The @ is here to avoid having to escape backslashes. This way, you can copy/paste directly from a regular expression editor.
- I made the decimal part a non capturing group (?: ... ), and gave it the ? (zero or one) repetition.
- Stripped blockquotes from the . (any character) class (they were not needed).

But, once again using a regular expression to validate values from 0 to 59.9 is a bad choice; the one you actually have does not validate numbers from 0 to 59.9. It validates numbers from 0 to 99.9.

There is a tool, Expresso[^], which is really useful when you want to build and test complex regular expressions. I encourage you to use it for your specific issue.

[Edit: Matt T Heffron: corrected the Modified Regex]
C#
Regex expressionToValidate_String_Structure = new Regex(@"^\d{1,2}(?:\.\d)?$");

You stripped the blockquotes from the . erroneously. That was the decimal point, not the (any character) class. Just escaping it with backslash is sufficient.
Also, you missed removing the unnecessary {1} quantifier from the digit in the fractional part. \d means exactly one unless quantified. ;)
 
Share this answer
 
v2
Comments
phil.o 13-Mar-14 16:48pm    
Arf, I now have to comment myself. Hope you will read it, Matt.
Indeed, you are right! Thank you for correction :)
(I intentionaly left the {1} quantifier, thinking OP may need to handle more digits sooner or later)

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