?
Solved

C# checking input value

Posted on 2005-04-25
11
Medium Priority
?
1,541 Views
Last Modified: 2012-05-05
I have a text box as an input field...
I basically want to check if the input is a 7 digit number or a 7 digit number such as this 333.4444. Separated by a dot.

Is there a way to do this in the code behind?
0
Comment
Question by:nryan30
  • 3
  • 3
  • 3
  • +1
11 Comments
 
LVL 7

Expert Comment

by:RealMrTea
ID: 13861751
If you are talking about an ASP.NET page...you can use a regular expression validator.

Otherwise you could use a regular expression.
0
 
LVL 7

Accepted Solution

by:
RealMrTea earned 375 total points
ID: 13861789
Example of regular expression...

333.4444
([0-9]{3}.[0-9]{4})

More Examples:
http://www.c-sharpcorner.com/3/RegExpPSD.asp

Later,
Eric
0
 
LVL 11

Expert Comment

by:jatinderalagh
ID: 13863946
string test = "333.4444";
                  int counter = 0;
                  for(int i=0; i<test.Length; i++)
                  {
                        if(char.IsNumber(test,i)) counter++;
                  }
                  if(counter == 7)
                  {
                        MessageBox.Show("Test Successfull.");
                  }
                  else
                  {
                        MessageBox.Show("Test failed.");
                  }
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
LVL 14

Expert Comment

by:puranik_p
ID: 13865322
Just an addition to jatinderalagh's solution.
also check for IsNumber for the whole number.
Otherwise "333A4444" will also show "Test Successfull"
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13869346
Would 3333.444 be okay? How about 33333.44?

@jatinderalagh:
Does IsNumber return true or false for decimal '.'.

Is this an international application?  Maybe the decimal is a comma ','.

Bob
0
 
LVL 11

Expert Comment

by:jatinderalagh
ID: 13872926
@Bob

[C#] public static bool IsNumber(char);
Indicates whether the specified Unicode character is categorized as a number.
Supported by the .NET Compact Framework.


[C#] public static bool IsNumber(string, int);
Indicates whether the character at the specified position in a specified string is categorized as a number.
Supported by the .NET Compact Framework.

As it supports the unicode characters. So it does not matter whether it's a international appplication or not.

Jatinder
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13875148
What I am trying to ask is:

What is the result of IsNumber(".") or IsNumber(",")?  Because you are not looking at the string as a whole, but each individual character.

Bob
0
 
LVL 11

Expert Comment

by:jatinderalagh
ID: 13875429
false
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 13876392
This should return false for 333A4444, but since it only counts for 7 numeric values, then this would be seen as valid (from puranik_p comment).

@puranik_p:
How would you check IsNumber with the entire string?  It is char.IsNumber, not string.IsNumber.

Code modificatiion from jatinderalagh's code (not for points):

      string test = "333A4444";

      bool valid = true;
      int counter = 0;

      char dec = System.Globalization.NumberFormatInfo.CurrentInfo.NumberDecimalSeparator.ToCharArray()[0];

      foreach (char ch in test.ToCharArray())
      {

        bool numeric = char.IsNumber(ch);

        if (ch != dec)
       
          if (!numeric)
          {
            valid = false;
            break;
          }
          else if (numeric)
            counter +=1;

      }

      if (valid && counter == 7)
        MessageBox.Show("Test successful.");
      else
        MessageBox.Show("Test failed.");

Bob
0
 
LVL 7

Expert Comment

by:RealMrTea
ID: 13876709
Actually...I think this expression would work a little better.
"[0-9]{3}[.][0-9]{4}"
0
 
LVL 14

Expert Comment

by:puranik_p
ID: 13882947
@Bob,
>It is char.IsNumber, not string.IsNumber
I was thinking about IsNumeric (VB) when I posted this.

Thanks for correcting.
Pura
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month16 days, 18 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question