[Solved] How do I make textbox to start with a number?

0 favourites
  • 8 posts
From the Asset Store
Forget about default textbox restrictions, you can create sprites atop of the textbox
  • Hey Guys!

    How do i make textbox to start with a number? I need to create a textbox for numbers only, but the first number want to be "9 "..

    .. to start with 9 always without delete it.

    Any help? Thanks!

  • Ok! Per the support forum read first. Please show code or .capx file. So we can see what is your issue and give guidance from there. I know I am overzealous and Like giving out free advice on how to fix stuff. But I to need to follow the rules. So no code = No Help. Sorry Jeremy C.

  • A slightly advanced technique would be to use regular expressions (regex) to check the contents of the box every tick, and if it doesn't match your specified expression (doesn't start with 9, or contains a letter) you can delete the offending characters or replace with a 9, ect.

    I would store the contents of a text field in a variable. Every time a user tries to input a character, I would test the new string against a regex - if it passes, the variable gets updated with the new string. If it doesn't, the text field gets replaced by whatever was previously in the variable.

    Carbin - sometimes people don't even know where to start, so there wouldn't be much to show in a capx If he needed help for something like building his specific regex, then it would be more useful to see what he has tried so far.

  • Guys, first of all thanks for your posts!

    Carbincopy -> Αre you read my topic title and my post carefully? Why ask about capx? I know the rules..

    So, we open construct 2, a new empty area, we add a text box and boom! What is the magic code? That's it!

    It is not about rules, is just a clean question. Thanks!

    P.S: 9 was an example.. I want to start with a letter "A", or a symbol.. Whatever, but can't delete this first number/letter. I don't want if user right something instead 9 to delete it, i want 9 for example to be as first always. Whatever user want to write, 9 to be first.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I solved by myself but it's 99% completed..

    First of all here is the capx: http://odiusfly.com/TextBox_v1.capx

    The problem is:

    1. If you start write something, it starts with 9. All good!

    2. If you use backspace to start remove the numbers until you clear all field, and try to start write again it starts with 9. All good again!

    3. BUT if you have already numbers and select all together, and instead use delete or backspace, you write a number, the number 9 don't appear as first number.

    Any ideas? Thanks again..

  • Maybe something like this?

    https://www.dropbox.com/s/b1oqul8f3igx1 ... .capx?dl=0

    How do we do it?

    First, set the type of the textbox to "Number" – that means only numbers can be entered, and on mobile devices, it shows the number-only keyboard.

    (pic: https://www.dropbox.com/s/jqmmrjkn3up7sw9/type.png?dl=0)

    Then, we every tick compare the first character of the textbox

    len(TextBox.Text, 1)[/code:htnsu6xz]
    Well, we compare an integer of that [code:htnsu6xz]int(len(TextBox.Text, 1))[/code:htnsu6xz] with number 9. Integer because we compare with 9 (number). I guess you could do it without integer when comparing to "9" (as text; note the quote marks).
    If it doesn't equal, we set the text of the TextBox to [code:htnsu6xz]"9"&TextBox.Text[/code:htnsu6xz]
    That means it adds number 9 to the start of the text and keeps the things you have already typed inside.
  • int(len(TextBox.Text, 1))[/code:3a16jeqx]
    

    grigrizljac i was just about to make a new thread when i see this thread luckily this is the line of code i was looking for lol

  • grigrizljac

    BIG THANKS!!! : -)

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)