Welcome to Dream.In.Code
Become a C# Expert!

Join 150,423 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,096 people online right now. Registration is fast and FREE... Join Now!




Random RegEx Question

 
Reply to this topicStart new topic

Random RegEx Question

killnine
25 Mar, 2008 - 08:11 AM
Post #1

D.I.C Head
**

Joined: 12 Feb, 2007
Posts: 114



Thanked: 5 times
My Contributions
I have a data grid that a user can enter information into. It has to be a number, but can be an integer (42) or a float number (8.66666666669). There is no set number of digits the user must enter, but I must check for valid input. One issue I had with using:

CODE

if (val == null || System.Text.RegularExpressions.Regex.IsMatch(val, @"[0-9]").Equals(false))
                        {
                            cancelEvent.Cancel = true;
                        }


was that it only matches the first character (therefore, "8f" would match true). Every regex tutorial I can find it far too specific ("this is what you do if you want to verify 5 characters that the user inputs").

All I want to do is check the entire user's input and verify each character is a number and include a decimal point. Thoughts?


User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Random RegEx Question
25 Mar, 2008 - 08:30 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 162 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
I believe the pattern you're looking for is /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;. You could create a method which uses this patters to validate the user input. I would do something like this (C#)


csharp

/// <summary>
/// method to validate users input. Will allow
/// for a single decimal point with as many nunmeric
/// value in front and behind the decimal
/// </summary>
/// <param name="value">value to validate</param>
/// <returns>true/false</returns>
private bool validateInput(string value)
{
string pattern = @"/(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/";
System.Text.RegularExpressions.Regex check = new System.Text.RegularExpressions.Regex(pattern);
//check for numeric characters
return check.IsMatch(ValueType);
}



You could also use the same patters on the client side with JavaScript


jscript

function validateInput(value)
{
//regular expression patters
var regEx = /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
//check for numeric characters
return regEx.test(value);
}



Hope that helps smile.gif
User is offlineProfile CardPM
+Quote Post

killnine
RE: Random RegEx Question
26 Mar, 2008 - 10:38 AM
Post #3

D.I.C Head
**

Joined: 12 Feb, 2007
Posts: 114



Thanked: 5 times
My Contributions
P.S. I think your auto-coder put in "ValueType" when you wanted to put in "value", right? (referring to your first bit of code).
You are the best! Thanks again for your continued support. =)

This post has been edited by PsychoCoder: 26 Mar, 2008 - 10:57 AM
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Random RegEx Question
26 Mar, 2008 - 10:58 AM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 162 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
I originally had the value being passed in as ValueType but thought that looked update that reference, sorry about that blush.gif

Glad I could help smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 08:38PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month