Hi!
I need to validate user input from a Tedit. I need to make sure that a number is entered and not an alphabetic character or punctuation. Short of 52+ if statements, I can't seem to get a handle on it. I would also like to produce an error box if anything other than a number is entered. I am using C++ Builder 5. Since I perform math operations on the numbers I've already written the code to convert the text to float. Any help would sure be appreciated!
Thanks,
Paul
Comments
Why cant you simply check the input from the component's OnChange event. Loop through the string and check every byte with isdigit(). My suggestion is that if any input is found invalid, remove it from the box and alert the user with a beep or something... constantly getting message boxes up the nose is just frustrating for the user.
I also have a faint memory of a property of the kind "numeric only", but I'm not sure if it was fot TEdit or something else...
int isdigit( int d );
this function is found in "ctype.h"
returns 0 if 'd' is not a digit 0-9, other wise it returns non-zero
i.e it returns false if not digit, and true if digit.
Don't forget to check the first character separately to see whether it represents a sign (+, -).
also, you can check if the ascii of each charcter is between 30 and 39.
if so, then it is digit, and if not, then it is not a digit.
To produce a message, u can use the very simple function
ShowMessage("any text here");
also, you don't have to convert text to float, you have a very useful library, try to use it
you have a function called StrToFloat( const AnsiString );
see you
Mohammad Nasim
: I also have a faint memory of a property of the kind "numeric only",
: but I'm not sure if it was fot TEdit or something else...
yeah i also remember that, but i think it was in MFC library not VCL
Mohammad Nasim
: int isdigit( int d );
: this function is found in "ctype.h"
: returns 0 if 'd' is not a digit 0-9, other wise it returns non-zero
: i.e it returns false if not digit, and true if digit.
: Don't forget to check the first character separately to see whether
: it represents a sign (+, -).
: also, you can check if the ascii of each charcter is between 30 and
: 39.
: if so, then it is digit, and if not, then it is not a digit.
:
: To produce a message, u can use the very simple function
: ShowMessage("any text here");
:
: also, you don't have to convert text to float, you have a very
: useful library, try to use it
: you have a function called StrToFloat( const AnsiString );
:
: see you
: Mohammad Nasim
Thanks Mohammad! I like that. I never thought of checking to see if a number, I was thinking to check to see if it was a letter!
something like:
ifEdit1->Text != ascii(31..39);
ShowMessage("Please enter numerals only");
The StrToFloat functions are what I am using to convert.
Paul
: a number, I was thinking to check to see if it was a letter!
: something like:
:
: ifEdit1->Text != ascii(31..39);
: ShowMessage("Please enter numerals only");
:
: The StrToFloat functions are what I am using to convert.
: Paul
:
:
well, i am not sure i got the whole message, [b]English is my second language[/b], but what i meant with ascii(30 to 39) is to check as following
[code]
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int len = Edit1->Text.Length();
for(int i=1; i<=len; i++) {
char c = Edit1->Text[i];
if( c <= 0x30 || c >= 0x39 ) { // if it is not number
ShowMessage( "not number" );
}
}
}
[/code]
of course putting a message box inside a loop is very stupid approach, but it makes the idea clear, and u can change that situation.
Sorry Sorry Sorry, i mentioned that numbers in ascii table lies between 30 and 39, but i forgot to mention that theses values in [b]HEX[/b]
Mohammad Nasim
: I need to validate user input from a Tedit. I need to make sure that
: a number is entered and not an alphabetic character or punctuation.
: Short of 52+ if statements, I can't seem to get a handle on it. I
: would also like to produce an error box if anything other than a
: number is entered. I am using C++ Builder 5. Since I perform math
: operations on the numbers I've already written the code to convert
: the text to float. Any help would sure be appreciated!
: Thanks,
: Paul
:
Hey I m sorry for this too delayed post.
Coz now only I joined this forum (also just started working in C++ builder)
For other Readers not to be misguided, here is my solutuon
Hope you are aware that there is a control called TMaskEdit dedicated for this kind of purposes.
If you still want to use TEdit try the following simple code instead.
Handle the KeyPress event handler of your edit box like below
:[code]:
#include
void __fastcall TForm1::Edit1KeyPress(TObject *Sender, char &Key)
{
std::string str = "+-1234567890.";
if( str.find(Key) == std::string::npos &&
Key !=VK_BACK )
{
ShowMessage("Non numeric is not allowed");
Key =0;
}
}
:[/code]:
OnKeyPress
[code]
void __fastcall Form1::edtKeyPress(TObject *Sender,
char &Key)
{
if(!(((int)Key >= 48 && (int)Key<=57) || Key == '-' || Key==''))
{
Key = 0 ;
}
}
[/code]
hope this help
Mysterious