Max attribute not respected when manually entering value - javascript

I have a input field for which I am trying to replicate hours in the day 0 - 24:
<input type="number" min="0" max="24" step="1" value="00" class="time-hours">
<input type="number" min="0" max="60" step="1" value="00" class="time-seconds">
When I click on the up/down chevrons, the max, and min I can go to are 24 and 0 as set in my attributes.
However, if I click on a cell, I can enter any number, e.g. 100.
How can I ensure only numbers between 0 and 24 can be entered?

If its a form submit, the browser will stop the user. But in case you really need the validation, you can do this:
$(".time-hours").keyup(function() {
if ($(this).val() > 24) {
$(this).val("24");
}
if ($(this).val() < 0) {
$(this).val("0");
}
});
$(".time-seconds").keyup(function() {
if ($(this).val() > 60) {
$(this).val("60");
}
if ($(this).val() < 0) {
$(this).val("0");
}
});
Here is the JSFiddle demo

If you want to use javascript you can simple use this below code:
<input type="number" min="0" max="24" step="1" value="00" class="time-hours" onblur="return minmax(this.value);">
<input type="number" min="0" max="60" step="1" value="00" class="time-seconds" onblur="return minmax(this.value);">
<script>
function minmax(val){
if(val > 24 || val < 0){
alert("Value cannot be grater than 24 and min than 0");
return false;
}
}
</script>
I have used onblur because whenever value changes in two boxes they will trigger a function and it will check textbox value if value is greater than 24 or min than 0 it will prompt an alert.
I have used alert you can use anything for your validations.
I hope it will help you.

You can perform this pure JavaScript solution:
<script>
ob = document.getElementById('f');
ob.onblur = function(){
if (ob.value*1 > ob.max*1 || ob.value*1 < ob.min*1){
ob.value = 24
}
}
</script>
Here you give the field an id. You are able to enclose it into a function to use it many fields.
Here is a demo

Related

Mix/max range applied to specific number input field

Using a solution from a relevant question, how can I use the following script to only work on a specific input by limiting the input range between 0 and 0.20? For example, I have two number inputs with the ID's "A" and "B", and I only want the following script to work on input id="A".
$('input').on('input', function () {
var value = this.value;
if (value !== '') {
value = parseFloat(value);
if (value < 0)
this.value = 0;
else if (value > 0.20)
this.value = 0.20;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" id="a" value="0" />
<input type="number" id="b" value="0" />
Use the ID of the specific element.
$('#a').on('input', function () {
Demo:
$('#a').on('input', function() {
var value = this.value;
if (value !== '') {
value = parseFloat(value);
if (value < 0)
this.value = 0;
else if (value > 0.20)
this.value = 0.20;
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="number" id="a" value="0" />
<input type="number" id="b" value="0" />
Alternatively, if you only need to support HTML5-compatible browsers you could ditch the JavaScript and simply use min and max attributes on the element itself. You can also use step to control how much the button increases the value by, which makes sense in a scenario where the input range is so small:
<input type="number" id="a" value="0" min="0" max="0.2" step="0.1" />
<input type="number" id="b" value="0" />
Change the selector from selecting all inputs to just the one you want e.g. $('#a').on('input', ...

input type="number" is not able to delete/clear on selecting the number in Firefox and IE

I have validation to accept the min and max value for an input type="number" field. Once I entered some input it is working according to the max and min value. What is my problem is I could not able to clear/delete an entered value in the number field. In textbox field, it is allowing to clear the value by using backspace and selecting the number and type new number. why number field is not working in that scenario. how to make it clear/delete the number in the number field.
<input type="number" name="WorkingDaysPA" id="WorkingDaysPA"
min="1" max="366" style="text-align: right"
onkeypress="if(this.value.length==3) return false;return (event.charCode == 8 || event.charCode == 0) ? null : event.charCode >= 48 && event.charCode <= 57"
/>
<input type="text" maxlength="3" min="1" max="366">
Here My fiddle: https://jsfiddle.net/MariNithya/fpd2y4f4/
On focus out event, I am assiging null as value;
Hope this helps
document.getElementById("validateNums").addEventListener("focusout", inputOutOfFocus);
function inputOutOfFocus() {
document.getElementById("validateNums").value = null;
}
<input type="number" maxlength="3" min="1" max="366" id="validateNums">
to clear the text field we use document.getElementById("exampleId").textContent = "";
whereas to clear number field use document.getElementById("exampleId").value =null ;
hope this solves your problem.

Jquery input text allow decimal and value between 0.00 and 100

Jquery input text allow decimal and value between 0.00 and 100
HTML :
<input name="score type="number" min="0.01" max="100" step="0.01">
is showing invalid when i enter "1111" but i want to validation after i click the submit button
<input type="text" name="score">
<input type="submit">
Script:
if($(this).val()<= 0.01 && ($(this).val() <=100)){ // allow 0.01 to 100 100
return true;
}
i want to allow only decimal in the Field
You can use HTML for this:
<input type="number" min="0" max="100" step="0.01">
If you don't want to use HTML:
$(document).on("ready", function() {
$("#form").submit(function(e) {
var scoreVal = $("#score").val();
if(parseInt(scoreVal) <= 0 || parseInt(scoreVal) > 100) {
e.preventDefault();
console.log("Wrong value was entered!");
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="form">
<input id="score" type="text" name="score">
<input type="submit">
</form>
#Martijn Bots solution seems fair to me, in case you want to do it otherwise, then you can check specifically for 0.00 and then correct your logic to validate range >=0.01 to 100
function validate()
{
if ( jQuery("#mytext").val() == "0.00"
|| ( $("#mytext").val() >= 0.01 && $("#mytext").val() <=100)
)
{
alert(true);
}
else {
alert(false);
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="mytext" type="text" name="score" onblur="return validate();">

Add placeholder instead of value in number and range input when value is 0 - Angular.js

I have a number and range input that are working in unison using same value using Angular.js value. When the value is set to 0 (which is what it is by default when page loads $scope.lbs_needed = 0;, I want to show a placeholder="0" whenever the value of the number or range input is set to 0 that way the 0 value isn't in front of the user's input without them having to manually delete default 0.
Here's my html:
<form>
<div class="form-group">
<label for="number">Pounds of nitrogen desired per acre:</label>
<input type="number" class="form-control number-input" id="number" ng-model="lbs_needed" ng-change="calculate2()" value="{[{lbs_needed}]}" min="0" max="500" value="{[{lbs_needed}]}" placeholder="0">
<input type="range" min="0" max="500" class="form-control" id="number" ng-model="lbs_needed" ng-change="calculate2()" value="{[{lbs_needed}]}" placeholder="0">
</div>
</form>
So, if I understand correctly you want the textbox to have a default value but also want it to be changeable instantly without users having to backspace over the default?
Try this:
var input = document.getElementById('change-default');
input.onfocus = function () {
if (input.value == 0) {
input.value = '';
}
}
input.onblur = function () {
if (input.value == '') {
input.value = '0';
}
}
<input type="number" placeholder="0" value="0" id="change-default">
What I'm doing is listening for an focus event, you could also use addEventListener instead of onfocus.
If the value of the input box is 0 you remove its value ready for the user to type theirs in.

Calculate the sum of multiple inputs - jQuery Range Slider

I am attempting to calculate the sum of multiple jQuery range sliders. There are many results on stackoverflow when it comes to this, most are not what I'm trying to accomplish, and others show 2 sliders as an example, when I try to add more it breaks (not sure what I'm doing really)..
Here is what I have, 6 sliders, and then I want it to calculate the overall rating after the last one.
http://fasterforms.com/images/111914.png
Here is a working fiddle, but as I said, when I try to either add onto the example it breaks, or it is for a "text" input and when I made it a jQuery "range" input it doesn't work.
http://jsfiddle.net/DULYW/2/
Here is the closest one I've got to work with 2 range sliders, but when I try to add more based off the code it doesn't work either.
<script type="text/javascript">
function calc(A,B,SUM) {
var one = Number(A);
if (isNaN(one)) { alert('Invalid entry: '+A); one=0; }
var two = Number(document.getElementById(B).value);
if (isNaN(two)) { alert('Invalid entry: '+B); two=0; }
document.getElementById(SUM).value = one + two;
}
</script>
Enter a number:
<input name="sum1" id="op1" value="" onChange="calc(this.value,'op2','result')" type="range" min="0" max="5" data-highlight="true" />
and another number:
<input name="sum2" value="" id="op2" onChange="calc(this.value,'op1','result')" type="range" min="0" max="5" data-highlight="true" />
Their sum is:
<input name="sum" value="" id="result" readonly style="border:0px;">
It always seems anything that works normally for me always refuses to work the same way once I add it to my jQuery mobile pages, any help would be awesome.
http://jsfiddle.net/DULYW/60/
Add on change event:
JS:
$('.add').change(function(){
var sum = 0
$('.add').each(function(){
sum += isNaN(this.value) || $.trim(this.value) === '' ? 0 : parseFloat(this.value);
});
$('#total').html(sum);
})
HTML:
<input class="add" name="sum1" type="range" min="0" max="5" data-highlight="true" />
<input class="add" name="sum1" type="range" min="0" max="5" data-highlight="true" />
<input class="add" name="sum1" type="range" min="0" max="5" data-highlight="true" />
<input class="add" name="sum1" type="range" min="0" max="5" data-highlight="true" />
<p id="total"></p>
For jQuery mobile you need to make sure that you put your code within an appropriate page event so that the widgets have been created. So if your sliders were within a data-role=page div with an id="page1", you could handle the pagecreate event and within it, run the addAll to get the initial total and then add a change handler to each of the sliders that also runs the addAll function:
$(document).on("pagecreate", "#page1", function () {
$(".add").on("change", function () {
addAll();
});
addAll();
});
function addAll() {
var sum = 0
$('.add').each(function (){
sum += isNaN(this.value) || $.trim(this.value) === '' ? 0 : parseFloat(this.value);
});
$('#total').html(sum);
}
DEMO
Both worked for me, but I ultimately switched mine to ezankers coding because it allowed me to have different names and they didnt have to be all the same.
http://jsfiddle.net/ezanker/DULYW/61/
$(document).on("pagecreate", "#page1", function () {
$(".add").on("change", function () {
addAll();
});
addAll();
});
function addAll() {
var sum = 0
$('.add').each(function (){
sum += isNaN(this.value) || $.trim(this.value) === '' ? 0 : parseFloat(this.value);
});
$('#total').html(sum);
}
Thanks for both of your help, you helped me out a lot.

Categories

Resources