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 wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

isNAN function in C# 1

Status
Not open for further replies.

jwpward

Programmer
Sep 5, 2003
45
GB
Hi

I'm looking for a bit of code that will force a value entered into a text box to be a number.

I know that in javascipt, for example, you could use something like:

Code:
if (isNaN(txtMonth.value)== true) {
		alert("Date of birth cannot contain letters or symbols");
	return;
}
..but I'm not sure of the best way to reproduce this in C#

Thanks alot in advance

 
check for keydown or keypress events and block any character which is not a number (or backspace,enter etc)
 
Regular expression... cast to an integer in a try block... those are both C# techniques (meaning, server-side). If you wish to do it client-side, then just use JavaScript isNaN.

Thomas D. Greer
 
NaN's are only applicable to floating-point types (Double and Single). The .net framework has these values (defined by the IEEE), and you check for them like this:
Code:
if (value == Double.NaN)
   // throw exception, etc.
Chip H.


____________________________________________________________________
Donate to Katrina relief:
If you want to get the best response to a question, please read FAQ222-2244 first
 
Hi

Thanks so much for all your replies. Very helpful indeed.

Chip H - thanks for the code. That's done the job.

Cheers

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top