PDA

View Full Version : CHeck if tbox Value is numeric



Swankee
29th October 2009, 23:20
If a user inputs a string into a textbox I'd like to check to see if the value was numeric or throw an error that the value was non numeric. So far I don't have anything aside from the error message. Thanks for your help



QString isNumber = "";
//grab value from lineEdit tb
isNumber = m_ui->lineEdit->text();

//is it a + / - ?
if (isNumber == "+" || "-")
{
//Warning message
m_ui->messageLbl->setText(m_ui->lineEdit->text() + " Is Not Valid. \n Enter a Numeric Value");
}

RSX
30th October 2009, 00:28
QString str = m_ui->lineEdit->text();
foreach (QChar c, str) {
if (c.isNumber()) {
// one of the characters in string is a digit
}
}

And this doesn't have sense. It will be always true as you are checking if "-" is non zero value, and in ASCII it's not.

if (isNumber == "+" || "-")

You can also use QValidator which will let user enter only specified characters into text box.

Swankee
30th October 2009, 02:41
QString str = m_ui->lineEdit->text();
foreach (QChar c, str) {
if (c.isNumber()) {
// one of the characters in string is a digit
}
}

And this doesn't have sense. It will be always true as you are checking if "-" is non zero value, and in ASCII it's not.

if (isNumber == "+" || "-")

You can also use QValidator which will let user enter only specified characters into text box.


Cool thank you that worked. I'm going to be working more with validation in the coming days. I'll check out QValidator in the morning. Thanks much! Swank