Tagged: TextInput Element
May 4, 2020 at 1:19 pm #5189
I have been trying to find some settings for the TextInput Elements in my code below :
1) How can I make sure that the “Continue” button doesn’t take effect if some of the TextInput Elements are not filled ?
2) Is there some way to adjust the TextInput answers to be strictly between 2 certain numbers ? i.e. : in the “age” TextInput below, if the number written is less than 18, it’s not validated.
newText("age", "<p></p>Age (years):") .print() , newTextInput("age") .settings.length(2) .print() .log() , newText("height", "<p></p>Height (cm):") .print() , newTextInput("height") .settings.length(3) .print() .log() , newText("weight", "<p></p>Weight (kg):") .print() , newTextInput("weight") .settings.length(3) .print() .log() , newText("<p></p>") .print() , newButton("Continue") .color("green") .print() .wait()
Thank you so much in advance for your help.
Sincerely,May 4, 2020 at 4:36 pm #5194
Yes, you can use .test.text to check the content of a TextInput element, and doing so in the wait command will only validate the click if the test is successful. In your case, you can use regular expressions to check for appropriate numbers, just replace your very last .wait() line with this:
.wait( getTextInput("age").test.text( /^(\d|1[0-8])$/ ) .failure( newText("Age should be a number between 0 and 18").print() ) .and( getTextInput("weight").test.text( /^\d+$/ ) .failure( newText("Weight should be a numeric value").print() ) ).and( getTextInput("height").test.text( /^\d+$/ ) .failure( newText("Height should be a numeric value").print() ) ) )
JeremyMay 5, 2020 at 4:29 am #5196
Thank you so much for your fast reply !September 14, 2020 at 8:07 am #6079
is there a way to adapt test.text() to test whether or not the textInput element isn’t empty? It seems like the most standard test for a textInput element, but I can’t seem to find the solution anywhere!
DanielaSeptember 14, 2020 at 11:28 am #6080
You can take two approaches: either use a negative test
testNot.text("")or a positive test using a regex
test.text(/[^\s]+/). The latter is more powerful, as it will only validate non-empty text, whereas the former would succeed even if the text only consists of spaces, for example
JeremySeptember 16, 2020 at 5:28 am #6126
that’s great, thank you again!
You must be logged in to reply to this topic.