Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datatype validation

Status
Not open for further replies.
Apr 11, 2002
193
IN
Hi,

I want to check if a string is a int or a number with 2 decimals or 4 decimals. I tried this
if (double.TryParse(dr[Col].ToString(),NumberStyles.Currency,CultureInfo.CurrentCulture, out result))

but it doesnt work for 4 decimals like "11.1245".

Similarly i want to check a string which might contain something like 20070518 which is a valid date for us. I want to check if this is a date in a if statement.

I am using C# 2.0.

Can anyone please help me with this.

Thanks,
Manish
 
If you want to check a string for a valid date, I would jsut do this (you really don't want to do it on your own I don't think).

Code:
try {
DateTime myDT = Convert.ToDateTime(myString);
}
catch (exception ex)//I forget the exact exception type, DateFormat?
{
MessageBox.Show("Invalid Date Entered")
}

To check how many decimal places you have, perhaps you could simply reverse teh string and take indexOf the '.' ?

-1, 3, and 5 will be acceptable values, all others will not if I understand you correctly.

Hope this helps,

Alex



Ignorance of certain subjects is a great part of wisdom
 
This is the time when you will want to look into Regular Expressions (Regex)

The Match() method is what you are looking for...
 
Hi Guys,

Thanks for the responses. Even i feel regex is a good option. But is there any expression to check if it is a date. The database will always return me a string like "20070718". So i want to differentiate date and numbers.

Thanks,
Manish
 
if the db returns yyyyMMdd then you will need something like this
Code:
public static DateTime? ConvertDate(string dateAsString)
{
   [COLOR=green]//validate string is not null[/color]
   if(string.IsNullOrEmpty(dateAsString))
   {
      return null;
   }

   [COLOR=green]//validate string is 8 characters[/color]
   if(dateAsString.length != 8)
   {
      return null;
   }

   [COLOR=green]//validate year[/color]
   int year = 0;
   if (!int.TryParse(dateAsString.SubString(0, 4), out year))
   {
      return null;
   }

   [COLOR=green]//validate month[/color]
   int month = 0;
   if (!int.TryParse(dateAsString.SubString(4, 2), out month))
   {
      return null;
   }

   [COLOR=green]//validate day[/color]
   int day = 0;
   if (!int.TryParse(dateAsString.SubString(6, 2), out day))
   {
      return null;
   }

   [COLOR=green]//validate date[/color]
   try
   {
      return new DateTime(year, month, day);
   }
   catch (FormatException)
   {
      return null;
   }
}
this is obviouly tedious. so this may work as well
Code:
DateTime date = DateTime.MinValue;
bool valid = DateTime.TryParse(DateAsString, out date);

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi,

Thanks for all the responses. The client just said that they have a different system which will return that kind of a format from a web service which they will be exposing. So i want to be rest assured that i will be ready for parsing the format.

I did try DateTime.TryParse but it didnt work.

Thanks,
Manish
 
chrissie1 said:
Why don't they expose a date type?
If I had a quid for every time I've found dates stored as strings on a database, I would have retired by now...

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Hey, that makes it easier to get your data into the database ;-)

I think that your web service may be provided by an Uncle Rico (See Definition #3)

Ignorance of certain subjects is a great part of wisdom
 
Guys,

The date is stored as datetime in the database. The client wants to display date in yyyymmdd format so they are sending it that way. The webservice sends me a string which contains xml. Can you recognize "20070511" is a date from a xml? Now the question is why i want to recognize it as a date if i just want to display it in the grid. I want to display all numbers as right aligned and everything else as left aligned. I hope that made sence.

Jason,

Thanks your method did work.

Regards,
Manish

 
If you're pulling it from XML then you should know what you're expecting. The tag should be something like "StartDate" in which you know you're looking for a date.

That is afterall the point of a tag in xml - to identify the data being stored.
 
Or they could send it as date. Since when do you do formatting on the "data"-side?

Only uncle rico saves his dates as string.

Formatting is done on the view side all the rest is "rubbish".

Christiaan Baes
Belgium

"My old site" - Me
 
>>>Formatting is done on the view side all the rest is "rubbish".

Said much more politely than I would have, Christiaan! ;-)




Ignorance of certain subjects is a great part of wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top