Results 1 to 3 of 3

Thread: CHeck if tbox Value is numeric

  1. #1
    Join Date
    Oct 2009
    Posts
    19
    Thanks
    2

    Default CHeck if tbox Value is numeric

    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

    Qt Code:
    1. QString isNumber = "";
    2. //grab value from lineEdit tb
    3. isNumber = m_ui->lineEdit->text();
    4.  
    5. //is it a + / - ?
    6. if (isNumber == "+" || "-")
    7. {
    8. //Warning message
    9. m_ui->messageLbl->setText(m_ui->lineEdit->text() + " Is Not Valid. \n Enter a Numeric Value");
    10. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Apr 2009
    Posts
    46
    Thanks
    4
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: CHeck if tbox Value is numeric

    Qt Code:
    1. QString str = m_ui->lineEdit->text();
    2. foreach (QChar c, str) {
    3. if (c.isNumber()) {
    4. // one of the characters in string is a digit
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Qt Code:
    1. if (isNumber == "+" || "-")
    To copy to clipboard, switch view to plain text mode 

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

  3. The following user says thank you to RSX for this useful post:

    Swankee (30th October 2009)

  4. #3
    Join Date
    Oct 2009
    Posts
    19
    Thanks
    2

    Default Re: CHeck if tbox Value is numeric

    Quote Originally Posted by RSX View Post
    Qt Code:
    1. QString str = m_ui->lineEdit->text();
    2. foreach (QChar c, str) {
    3. if (c.isNumber()) {
    4. // one of the characters in string is a digit
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Qt Code:
    1. if (isNumber == "+" || "-")
    To copy to clipboard, switch view to plain text mode 

    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

Similar Threads

  1. Help: How to save Check box state
    By Garibalde in forum Qt Programming
    Replies: 4
    Last Post: 1st July 2009, 20:24
  2. Replies: 0
    Last Post: 2nd May 2008, 07:57
  3. Check a point inside or outside a road?
    By kstking in forum Qt Programming
    Replies: 1
    Last Post: 15th October 2007, 18:48
  4. Check and Uncheck on a Dir Tree?
    By vishal.chauhan in forum Qt Programming
    Replies: 2
    Last Post: 3rd July 2007, 11:55
  5. How to check a file for changes since last save
    By nmather in forum General Programming
    Replies: 2
    Last Post: 21st April 2007, 23:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.