3 Replies - 1454 Views - Last Post: 26 March 2008 - 11:58 AM Rate Topic: -----

#1 killnine   User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 161
  • Joined: 12-February 07

Random RegEx Question

Posted 25 March 2008 - 09:11 AM

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:

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?

Is This A Good Question/Topic? 0
  • +

Replies To: Random RegEx Question

#2 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Random RegEx Question

Posted 25 March 2008 - 09:30 AM

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#)


/// <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


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 :)
Was This Post Helpful? 0
  • +
  • -

#3 killnine   User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 161
  • Joined: 12-February 07

Re: Random RegEx Question

Posted 26 March 2008 - 11:38 AM

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 March 2008 - 11:57 AM

Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1663
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Random RegEx Question

Posted 26 March 2008 - 11:58 AM

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

Glad I could help :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1