let x=createInput("", "number");
creates a textbox with tiny scrollbars to scroll through the numbers and can also have negative numbers.
I want to create a textbox so that people can put in their age.
let x=createInput("", "number");
creates a textbox with tiny scrollbars to scroll through the numbers and can also have negative numbers.
I want to create a textbox so that people can put in their age.
Canât users type into the textbox as well?
They can and they can also get to negative numbers as well.
So your goal is to restrict to positive numbers only?
I would recommend starting with a google search. Something like âhtml number input restrict valuesâ will return a ton of helpful results.
Good luck!
It does if I were coding in html.
Itâs alright, I wrote a function to check the input for numbers only.
@GoToLoop do you mind checking my isNumber function? It works and I canât seem to be able to âbreakâ it but I want to know if itâll always work.
Thanks.
If youâre coding in p5.js, then youâre coding in HTML and JavaScript.
Anything you can do in HTML and JavaScript, you can do in p5.js.
I would just go with the HTML standard, as @Kevin is suggesting. You could use the min
and max
attributes to set a reasonable age span. Donât like the scrollbars? Turn off the number input spinners.
But if you are going the custom way⌠Your isNumber
function right now accepts input other than numbers.
Maybe you could do something like this instead?
function setup() {
let age = createInput('');
age.input(enforcePositiveInteger);
}
function enforcePositiveInteger() {
const userInput = this.value();
const onlyDigits = userInput.replace(/^0+|[^\d]/g, '');
if (userInput !== onlyDigits) {
this.value(onlyDigits);
}
}
Iâm glad I was suspicious of that function.
Your enforcePositiveInteger() is working, thanks. I need to figure out that regex though. I think I would much rather use whatâs in the link you posted:
input[type=number]::-webkit-inner-spin-button,
input[type=number]::-webkit-outer-spin-button {
-webkit-appearance: none;
margin: 0;
}
coupled with some simpler code (I donât like regular expressions because I donât understand them, yet). But Iâm too much of a beginner to know how to use css code. I threw it at the bottom of my style.css file and nothing happened.
@Kevin Iâm sure what you say is true, however, as a beginner I can tell you that there is a gap in the âhow-toâ out there between html and p5.js. So, I have found code out there in html that claims to do what I want, however, it is very unclear to me how to implement it in p5.js. This is one of the reasons I was looking for all the methods that are available to an object yesterday, as you might remember.
Yeah, I feel for you, regular expressions sure is a bit awkward in the beginning. But I do encourage you to pursue learning at least the basics. Learn Regex the Easy Way looks like a good starting point.
But Iâm too much of a beginner to know how to use css code. I threw it at the bottom of my style.css file and nothing happened.
Sorry about that, I didnât verify the CSS. It looks like Firefox is a special snowflake. Thatâs what so charming with browsers, they donât always work the same way. Append this snippet of CSS as well:
input[type=number] {
-moz-appearance: textfield;
}