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
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: If the player makes no errors, it attempts to forward them to another passage—though that could be changed.
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>>
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: Notice that you did not specify a conditional expression. Rectify that to resolve the error.
Here's some code, using TME's excellent example as a guide:
That is a really good point. Thanks for the catch.