Re: Check for valid number
Posted by webmaster Guido on March 18, 2001 at 20:02:20:
In Reply to: Check for valid number posted by Amit Kr Shaha on March 16, 2001 at 02:33:55:
: What is the function to check whether a text box contains a valid number or not(float,integer) ? ---------- You can do this with the procedure Val(S, V, Code). Example: Function IsValidReal(S: string): Boolean; var V: real; // result of conversion Code: integer; // position of first invalid character begin Val(S, V, Code); Result := (Code = 0); end; Another example: Function IsValidInteger(S: string): Boolean; var V: integer; // result of conversion Code: integer; // position of first invalid character begin Val(S, V, Code); Result := (Code = 0); // TRUE if no error end; Example of using this: if IsValidReal(Edit1.Text) then ... But be careful, this only works if the decimal separator is a POINT, meaning: 3,1415 is flagged as invalid! (Americans think everybody uses a decimal *point*, they never heard of decimal *comma*). If your application will be used on systems with decimal point as well as systems with a decimal comma, for "real" numbers you have to do some conversions: replace the comma with a point before checking for validity. Or use a different approach with TRY... EXCEPT... and StrToFloat() -- I think that StrToFloat looks at Windows' decimal seperator. For more info on decimal points and comma's, have a look at our Delphi Tutorial "EuroConvertor" :)
|
|