Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How do I make a text box which only accepts numbers?  (Read 7405 times)

0 Members and 2 Guests are viewing this topic.

Ezert

  • Newbie
  • *
  • Posts: 12
    • View Profile
How do I make a text box which only accepts numbers?
« on: December 05, 2018, 10:35:35 pm »
Hi, everybody! o/

My question is quite simple:

How do I code a text box which only accepts numbers?

I'm trying to make a dice roller, and I'm having trouble with it...

Thanks in advance.
« Last Edit: December 05, 2018, 10:45:10 pm by Ezert »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: How do I make a text box which only accepts numbers?
« Reply #1 on: December 05, 2018, 11:56:27 pm »
It isn't clear in your question whether or not your struggling to make a text box, or if you've solved that part but are now struggling to limit it to only accepting numbers. Can you provide more details on exactly what you're having trouble with and what you've tried?

To create a basic text box you'll probably want to draw a sf::RectangleShape to represent the box, sf::Text to show the text inside of the text box, and use events to know when the textbox has been clicked on (for focus) and to know when the user is typing something. You can specifically listen for the the sf::Event::TextEntered event and only process ones that contain a number.

Alternatively you could use an existing GUI library like Imgui, TGUI, or SFGUI.

Ezert

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: How do I make a text box which only accepts numbers?
« Reply #2 on: December 06, 2018, 12:17:41 am »
It isn't clear in your question whether or not your struggling to make a text box, or if you've solved that part but are now struggling to limit it to only accepting numbers. Can you provide more details on exactly what you're having trouble with and what you've tried?

To create a basic text box you'll probably want to draw a sf::RectangleShape to represent the box, sf::Text to show the text inside of the text box, and use events to know when the textbox has been clicked on (for focus) and to know when the user is typing something. You can specifically listen for the the sf::Event::TextEntered event and only process ones that contain a number.

Alternatively you could use an existing GUI library like Imgui, TGUI, or SFGUI.

I'm sorry for not expressing myself clearly ><
It was my bad english...
I'm struggling to limit the text box to only accept numbers... I had no problem coding the text box itself.
« Last Edit: December 06, 2018, 12:19:41 am by Ezert »

Kanoha

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: How do I make a text box which only accepts numbers?
« Reply #3 on: December 06, 2018, 12:46:45 pm »
I believe you can use std::regex_match (c++11) to check if the caracters entered are digits. The actual pattern would be something as simple as this: "^\d$". You can then check for every key that is typed if it is a digit and not write it if it is not.

Ezert

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: How do I make a text box which only accepts numbers?
« Reply #4 on: December 06, 2018, 08:42:31 pm »
I believe you can use std::regex_match (c++11) to check if the caracters entered are digits. The actual pattern would be something as simple as this: "^\d$". You can then check for every key that is typed if it is a digit and not write it if it is not.

Thank you! I wasn't familiarized with regex! I don't know how to use this yet, but you put me on the right path. All I need to do now is read about regex...

Thank you again! :)
o/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How do I make a text box which only accepts numbers?
« Reply #5 on: December 06, 2018, 09:04:59 pm »
If you don't want/need to support corner-case Unicode stuff (I don't even know if there is, for digits), there are much simpler solutions for checking if a character is a digit:

if (std::isdigit(c))

or

if ((c >= '0') && (c <= '9'))
Laurent Gomila - SFML developer

Ezert

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: How do I make a text box which only accepts numbers?
« Reply #6 on: December 07, 2018, 09:21:11 pm »
I believe you can use std::regex_match (c++11) to check if the caracters entered are digits. The actual pattern would be something as simple as this: "^\d$". You can then check for every key that is typed if it is a digit and not write it if it is not.

Hi.
I tried to implement regex in my code, but it still doesn't work...
The problem is that the text box stops to accept any input when I try to apply the regex feature...
If it's not too much to ask, I wish you could take a look at my code...


[...]

                case Event::TextEntered: {

                    regex isI ("[[digit]]+"); //regex deffinition

                    if (e.text.unicode == '\b'){ //backsapace

                            if (iNbr.size() > 0)
                            {
                            iNbr.erase(iNbr.size() -1);
                            n.setString(iNbr);
                            }

                    } else if (regex_match(iNbr, isI)) { //regex_match

                        iNbr += e.text.unicode;
                        n.setString(iNbr);

                    }

                break;}

[...]

 

If necessary, here is the entire code:
(click to show/hide)

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: How do I make a text box which only accepts numbers?
« Reply #7 on: December 07, 2018, 09:32:28 pm »
I may be misunderstanding your code, but why this
else if (regex_match(iNbr, isI))
 
instead of something like this?
else if (regex_match(e.text.unicode, isI))
 


Also, did you try out Laurent's suggestion for a simpler approach?

Ezert

  • Newbie
  • *
  • Posts: 12
    • View Profile
Re: How do I make a text box which only accepts numbers?
« Reply #8 on: December 08, 2018, 12:30:57 am »
I may be misunderstanding your code, but why this
else if (regex_match(iNbr, isI))
 
instead of something like this?
else if (regex_match(e.text.unicode, isI))
 

What a gross mistake!
You were right! It was the main problem! xP
Well, even then I had other problems: when I replaced "iNbr" by "e.text.unicode", occurred an incompatibility problem. So I tried to use isdigit again. I'd tried to use isdigit before, but the same problem (text box froze) had happened. That was happening because I was committing the same mistake writing isdigit(iNbr). Then, I did this:

[...]

                  } else {

                        string check = "";
                        check += e.text.unicode; //key pressed is armazenated in check.

                        //if check is a digit, its value is added to iNbr:
                        if (isdigit(check)){

                            iNbr += check;
                            n.setString(iNbr);

                        }

                    }

[...]


 

But another problem occurred: isdigit only accepts char. Nevertheless, it was easy to fix:

[...]

                    } else {

                        char check = '\0';
                        check += e.text.unicode; //key pressed is armazenated in check.

                        //if check is a digit, its value is added to iNbr:
                        if (isdigit(check)){

                            iNbr += check;
                            n.setString(iNbr);

                        }

                    }

[...]


 

Now it's working! ><'

Well, thank you very much. You all helped me a lot :) o/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: How do I make a text box which only accepts numbers?
« Reply #9 on: December 08, 2018, 08:30:16 am »
It's a strange way to convert, and it's not entirely safe. I would rather do this (std::isdigit wants an int, not a char):

if ((e.text.unicode < 128) && std::isdigit(static_cast<int>(e.text.unicode)))
Laurent Gomila - SFML developer

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: How do I make a text box which only accepts numbers?
« Reply #10 on: December 09, 2018, 08:31:17 pm »
I haven't read everything posted, but wouldn't it make more sense to only accept key input from number keys rather than listening to TextEntered? Send sf::Keyboard::Key as a parameter to your textbox class when keys 0 to 9 are pressed.

Kanoha

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: How do I make a text box which only accepts numbers?
« Reply #11 on: December 09, 2018, 10:15:05 pm »


I haven't read everything posted, but wouldn't it make more sense to only accept key input from number keys rather than listening to TextEntered? Send sf::Keyboard::Key as a parameter to your textbox class when keys 0 to 9 are pressed.

To me, it makes more sense to use the Event::TextEntered approach, first because it is easier to implement, and secondly because it has been created for this purpose  unlike sf::Keyboard (I think so at least).

Sent from my SM-G950F using Tapatalk


G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: How do I make a text box which only accepts numbers?
« Reply #12 on: December 10, 2018, 02:59:17 am »
Definitely use the TextEntered event. You don't care how the user entered the number, for 1 it could be Numpad1, Num1, alt+49, or whatever.

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: How do I make a text box which only accepts numbers?
« Reply #13 on: December 10, 2018, 11:46:53 am »
OK I see your points; but if it were me, just for simplicity's sake, I would just paste this one line of code into pollEvents() function for 0 to 9 and be done with it.

case sf::Keyboard::Num0: case sf::Keyboard::NumPad0: objTextBox.input(0); break;

Kanoha

  • Newbie
  • *
  • Posts: 19
    • View Profile
    • Email
Re: How do I make a text box which only accepts numbers?
« Reply #14 on: December 11, 2018, 07:18:36 am »
OK I see your points; but if it were me, just for simplicity's sake, I would just paste this one line of code into pollEvents() function for 0 to 9 and be done with it.

case sf::Keyboard::Num0: case sf::Keyboard::NumPad0: objTextBox.input(0); break;
That is not beautiful. You really should use events in this case, but I guess it's up to you.
« Last Edit: December 11, 2018, 07:44:01 am by Kanoha »