Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sugarcube Disallow Certain Textbox input

Hello, I am new and using SugarCube 2.14.0

I was watching tutorials but i cant find one on how to make sure that u can disallow certain things to be input into a textbox. Say i am trying to ask for a name and they put numbers in the box how would i say make it not allow that? I am sorry about the newb question, but i am indeed a nebwbie.

Comments

  • edited April 2017
    Something like this should work:
    ::enterName
    <<textbox "$name" "Name" "checkName" autofocus>>
    
    ::checkName
    <<set _testStr to /\d/.test($name)>>\ 
    /% /\d/.test() will be true if any digits (0-9) are found %/ \
    <<if _testStr>>\
    <<print $name>>
    
    Please do not enter any numbers in your name!
    
    <<display "enterName">>
    <<else>>\
    <<print $name>>
    
    Good!
    <</if>>
    
  • There's no real need to use multiple passages here.

    It would probably have been better to declare what's allowable, as that's generally easier to target than what's not.

    I'd suggest something like the following, which only allows letters—though that could be changed:
    What is your name?
    <<textbox "$name" "" autofocus>> \
    <<button "Confirm">>
    
    <<set $name to $name.trim()>>
    
    <<if $name is "">>
    	<<replace "#textbox-error">>Please enter a name.<</replace>>
    <<elseif /[^A-Z]/i.test($name)>>
    	<<replace "#textbox-error">>Names may only contain letters.<</replace>>
    <<else>>
    	<<replace "#textbox-error">><</replace>>
    	<<goto "THE_NEXT_PASSAGE">>
    <</if>>
    
    <</button>>
    <span id="textbox-error"></span>
    
    If the player makes no errors, it attempts to forward them to another passage—though that could be changed.
  • This works! Now I am trying to get it to go to different passages depending on the imput of textbox's, like if they type in a certain thing it will change the story, but it keeps giving me an error.


    here is the code im using


    <<textbox "$playerage" "" autofocus>> \
    <<button "Confirm">>

    <<set $playerage to $playerage.trim()>>

    <<if $playerage is "">>
    <<replace "#textbox-error">>Tell an age in years.<</replace>>
    <<elseif /[^0-9]/i.test($playerage)>>
    <<replace "#textbox-error">> Tell age in years, please.<</replace>>
    <<elseif>>
    <<replace "#textbox-error">> Really?<</replace>>
    <<elseif $playerage is "000">>
    <<replace "#textbox-error">> Thats Impossible!
    <<goto "Game Over Dont Exist">> <</replace>>
    <<elseif $playerage is "001-020">>
    <<replace "#textbox-error">>Really?
    <<goto "Too Young">> <</replace>>
    <<elseif $playerage is "01-20">>
    <<replace "#textbox-error">>Really?
    <<goto "Too Young">> <</replace>>
    <<elseif $playerage is "1-20">>
    <<replace "#textbox-error">>Really?
    <<goto "Too Young">> <</replace>>
    <<elseif $playerage is "50-100">>
    <<replace "#textbox-error">>Really?
    <<goto "WhySooOld">> <</replace>>
    <<else>>
    <<replace "#textbox-error">><</replace>>
    <<goto "Show Age">>
    <</if>>


    <</button>>
    <span id="textbox-error"></span>

    <<script>>
    postdisplay["textboxMaxlength"] = function (taskName) {
    /* Limit textbox input elements to 3 characters. */
    $(".macro-textbox").attr("maxlength", 3);

    /* Make this a single-use task.*/
    delete postdisplay[taskName];
    };
    <</script>>
  • Please use the code tag—it's C on the editor bar—when posting code or markup.


    The error message states that your second <<elseif>> is missing its conditional expression. Did you try looking at it as directed by the error message? If you do, you'll find the following:
    <<elseif>>
    
    Notice that you did not specify a conditional expression. Rectify that to resolve the error.
  • edited May 2017
    You could just turn the string into a number right off the bat and get rid of the postdisplay.

    Here's some code, using TME's excellent example as a guide:
    <<textbox "$age" "" autofocus>>
    <span id="textbox-error"></span>
    
    <<button "confirm">>
      /% parseInt() turns a sting into a number %/
      <<set $age to parseInt($age, 10)>>
      /% truncate any fractional numbers, e.g. 23.456 will become 23 %/
      <<set $age to Math.trunc($age)>>
    
      <<if isNaN($age)>>
        /% isNaN() returns true if the provided value is not a number %/
        <<replace '#textbox-error'>>\
          Please enter a number.\
        <</replace>>
    
      /% from here, just check the number %/
      <<elseif $age lt 10 or $age gt 200>>
        <<replace '#textbox-error'>>\
          You're hilarious.\
        <</replace>>
    
      <<elseif $age lt 20>>
        <<replace '#textbox-error'>>\
          Too young.\
        <</replace>>
    
      <<elseif $age gt 50>>
        <<replace '#textbox-error'>>\
          Too old.\
        <</replace>>
    
      <<else>>
        /% value is a number between 20 and 50, inclusive, and is accepted %/
        <<goto 'next passage'>>
      <</if>>
    
    <</button>>
    
  • edited May 2017
    Chapel wrote: »
      /% parseInt() turns a sting into a number %/
      <<set $age to parseInt($age, 10)>>
      /% truncate any fractional numbers, e.g. 23.456 will become 23 %/
      <<set $age to Math.trunc($age)>>
    
    The second <<set>> in that example—the one with the call to Math.trunc()—is superfluous. The parseInt() function only returns integers or NaN. Integers are whole numbers, having no fractional part, thus there is nothing to truncate.
  • The second <<set>> in that example—the one with the call to Math.trunc()—is superfluous. The parseInt() function only returns integers or NaN. Integers are whole numbers, having no fractional part, thus there is nothing to truncate.

    That is a really good point. Thanks for the catch.
Sign In or Register to comment.