how to limit the textbox input to numeric 0-10 in html - javascript

hi i have a textbox in html and i want a user to enter the numeric values only which will be for eg. 0,1,2,3,4,5,6,7,8,9,10 and if a user enters 11 it should show an alert box showing "only 0-10 is allowed" since i am a newbie
so far i have this script
SCRIPT language=Javascript>
function validateForm()
{
var x=document.form["tst"]["num"].value;
var y = parseInt( x , 10 )
var i=15;
if (y>i)
{
alert("Range is Between 0-10");
return false;
}
}
</script>
here is my textbox code,
<INPUT id="num" name="num" onkeypress="return validateForm()" size="2" maxlength="2" type="text" >

You could use an HTML5 input type for ease:
<input type="number" min="0" max="10">
This could potentially remove the need for ghastly modal popups.

Try to change var i=15; to var i=11;

<input type="number" name="quantity" min="0" max="10">

Related

how do i add a number limit in an input field?

How can i have a number limit? im trying to make it so that you can only type the number 1-100 in "app-benutzer" and 1-15 in "backend-benutzer" also is there a way to display these numbers in the input field without typing them so it would show the user that you can only type the number from 1-15.
html:
<body>
App-Benutzer: <input type="number" min="1" max="100" class='appuser'></input><br>
Backendbenutzer: <input type="number" min="1" max="15" class='backenduser'></input><br>
<button class='calcit'>Berechnen</button><br>
<span class='summe'>0.00</span><br>
<script src="./app.js"></script>
</body>
js:
const btncalc = document.querySelector('.calcit');
const summetext = document.querySelector('.summe');
btncalc.addEventListener('click', function(){
var backendanzahl= document.getElementsByClassName("backenduser")[0].value;
var appanzahl= document.getElementsByClassName("appuser")[0].value;
var mytext="Anzahl der Summe:" + (+backendanzahl * 35 + +appanzahl * 7.5) ;
summetext.textContent = mytext;
});
You can achieve this only by using HTML.
This way, you can't really write anything more than what you specify in your terms.
Clarification:
Placeholder is the attribute that you use to put some text inside of your input field, but that text will disappear as soon as you click on your element.
oninput is the Event that occurs every time that value of your input element has changed. So basically, each time you write 1 number/letter/whatever, oninput event will trigger.
That's where we put our condition.
this.value - represents what we write inside of our input element.
our value (this.value) = if (this.value > 100) (? represents something like return) return 100 ( (:) is like else) else return this.value
this.value = this.value > 100 ? 100 : Math.abs(this.value)
is equivalent to
if(this.value > 100) //this.value is what we write inside of the field
return 100;
else
return this.value;
<body>
App-Benutzer: <input type="number" min="1" max="100" oninput="this.value = this.value > 100 ? 100 : Math.abs(this.value)" class='appuser' placeholder='1-100'></input><br>
Backendbenutzer: <input type="number" min="1" max="15" oninput="this.value = this.value > 15 ? 15 : Math.abs(this.value)" class='backenduser' placeholder='1-15'></input><br>
<button class='calcit'>Berechnen</button><br>
<span class='summe'>0.00</span><br>
<script src="./app.js"></script>
</body>
You can use placeholder attribute of input field to put any text you want. This text will disapear once the user starts to edit the field. In you case you may may sat a placeholder to visualise "1-15".
<body>
App-Benutzer: <input type="number" min="1" max="100" class='appuser' placeholder="Input a value from 1 to 100"></input><br>
Backendbenutzer: <input type="number" min="1" max="15" class='backenduser' placeholder="Input a value from 1 to 15"></input><br>
<button class='calcit'>Berechnen</button><br>
<span class='summe'>0.00</span><br>
<script src="./app.js"></script>
</body>

Set input value with Javascript on outside click

i want to set a max value to a number input. I used the "max" HTML attribute but the user can write a number over the max value anyway. Is there any way to reset the value of that input to the maximum value I set it, after the user sets it beyond the maximum value? Maybe when the user clicks outside that input?
Here is my code
<input type="number" name="days" id="days" min="1" max="365" value="<?php echo $ipq[0]['valueParam']?>">
You might want to consider using a range control along with the value of the range being shown in a span element next to it. This way only the value range you specify is allowed (which is really what the range control is for).
const output = document.getElementById("output");
document.getElementById("days").addEventListener("input", function(){
output.textContent = this.value;
});
<input type="range" name="days" id="days" min="1" max="365" value="1"><span id="output">1</span>
The max attribute is validated on form submit. AS you can see in the snippet below the user will get a message when he tries to submit the form.
<form>
<input type="number" name="days" id="days" min="1" max="365" value="366">
<input type='submit'>
</form>
If you want to prevent the user for inputting a value over 365 you can use js to do that. The snippet below shows how to prevent values over 365 for all number inputs.
document.querySelectorAll('input[type="number"]').forEach((el) => el.addEventListener('input', (e) => {
let inputEl = e.currentTarget;
if(parseFloat(inputEl.value) > parseFloat(inputEl.max)) {
inputEl.value = inputEl.max;
}
}));
<form>
<input type="number" name="days" id="days" min="1" max="365" value="366">
<input type='submit'>
</form>

number in input field not higher than other input field

I have two input fields in my contact form, in both fields it's only possible to fill in a number. My question is, is there a way that the number in field 1 can never be higher than the number in field 2?
I have looked everywhere but can't find a solution. Is this even possible (with Javascript)? Hopefully somebody has a solution or can send me in the right direction.
<form>
<input type="number" class="fieldone" min="0" max="16" step="0.1">
<input type="number" class="fieldtwo" min="0" max="16" step="0.1">
</form>
function forceLimit(e) {
var limit = e.target.getAttribute("max");
if(e.target.value > limit)
e.target.value = limit;
}
function handleLimitChange(e) {
var limit = e.target.value;
var fieldOne = document.querySelector('#field-one');
if(fieldOne.value > limit) {
fieldOne.value = limit;
}
fieldOne.setAttribute('max', limit);
}
<form>
<input type="number" id="field-one" class="fieldone" onchange="forceLimit(event)" min="0" max="16" step="0.1">
<input type="number" id="field-two" class="fieldtwo" onchange="handleLimitChange(event)" min="0" max="16" step="0.1">
</form>
You can use jQuery to check that always the value in field 2 is greater or equal to field 1:
$('.fieldone').change(function(){
checkValue();
});
$('.fieldtwo').change(function(){
checkValue();
});
function checkValue(){
var maxValue = $('.fieldtwo').val();
if(maxValue > 0 && $('.fieldone').val() > maxValue){
$('.fieldone').val(maxValue);
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form>
<input type="number" class="fieldone" min="0" max="16" step="0.1">
<input type="number" class="fieldtwo" min="0" max="16" step="0.1">
</form>

Addition/subtraction to a specific value to user input

Is it possible to have a textbox where the user will input a number and in another textbox, which will automatically add 5 to the value of the first textbox and subtract 5 to the value in the third textbox?
For example:
User input: 10
2nd textbox: 15
3rd textbox: 5
Please help
You can do it like this:
<input type="text" id="input1" onkeyup="setValues(this.value);">
<input type="text" id="input2">
<input type="text" id="input3">
<script type="text/javascript">
function setValues(value){
document.getElementById('input2').value = (parseInt(value)+5);
document.getElementById('input3').value = (parseInt(value)-5);
}
</script>
you can find solution Link.
HTML Part
<input type="number" id="input1">
<input type="number" id="input2">
<input type="number" id="input3">
JS Part
$('#input1').bind('click keyup', function(event) {
$('#input2').val(parseInt(this.value)+5);
$('#input3').val(parseInt(this.value)-5);
})

javascript: how can I extract the values entered in this form?

This is not working for me. Can anyone tell me what I'm doing wrong?
function showValues() {
var uIwD;
uIwD = document.getElementById(uInputs.fWD);
alert ("You entered: " + uIwD);
}
<form id="uInputs">
Enter a value in the first field, then press Submit:<br>
<input type="number" id="fWD" value="" min="0" max ="6">
Weekday (0 to 6 for Sun to Sat)<br>
<input type="number" name="fHr" value="" min="0" max ="23">
Hour (0 to 23)<br>
<input type="number" name="fMins" value="" min="0" max ="59">
Minutes (0 to 59)<br>
<input type="number" name="fSecs" value="" min="0" max ="59">
Seconds (0 to 59)<br>
<input type="number" name="fWOffset" value="-6" min="-12" max ="12">
wOffsetHours (-12 to 12)<br>
<input type="button" value="Submit" onclick="showValues()">
</form>
I enter a value of 3 in the first input field, then I click "Submit" and I get a js alert popup saying "You entered: null"
I have tried replacing name= with ID= in the first input field, and then using just that ID in GetElementbyID(), but the result is the same.
Here is a working page where you can see the result. (I tried making a fiddle for it, but when I run it in jsfiddle, I get no alert message at all. It would seem js alert pop-ups don't work in jsfiddle.)
You are accessing it wrong. id should be a string and then you can access the Childs with id's as properties.
For example to test the first input box
function showValues() {
var uIwD;
var uIwD = document.getElementById("uInputs");
alert ("You entered: " + uIwD.fWD.value);
}
</head>
<body>
<form id="uInputs">
Enter a value in the first field, then press Submit:<br>
<input type="number" id="fWD" value="" min="0" max ="6">
Weekday (0 to 6 for Sun to Sat)<br>
<input type="number" name="fHr" value="" min="0" max ="23">
Hour (0 to 23)<br>
<input type="number" name="fMins" value="" min="0" max ="59">
Minutes (0 to 59)<br>
<input type="number" name="fSecs" value="" min="0" max ="59">
Seconds (0 to 59)<br>
<input type="number" name="fWOffset" value="-6" min="-12" max ="12">
wOffsetHours (-12 to 12)<br>
<input type="button" value="Submit" onclick="showValues()">
</form>

Categories

Resources