I have x number of textboxes with same class name. I want the user to enter dates in them and each date should be greater than the previous one. How can I achieve this?
jQuery -
jQuery.validator.addMethod("greaterrule", function(value, element, param) {
return $(param).not(element).get().every(function(item) {
return $(item).val() < value;
});
}, "Please specify a greater value");
jQuery.validator.addClassRules("dept_date", {
greaterrule: ".dept_date"
});
HTML -
First Date <input type="text" name="first_date" class="dept_date"> <br>
Second Date <input type="text" name="second_date" class="dept_date"> <br>
Third Date <input type="text" name="third_date" class="dept_date"> <br>
Fourth Date <input type="text" name="fourth_date" class="dept_date"> <br>
Try using prevAll to check against all the dates before and use Date to compare:-
jQuery.validator.addMethod("greaterrule", function(value, element, param) {
return $(element).prevAll(param).get().every(function(prev) {
return new Date($(prev).val()) < new Date($(element).val());
});
}, "Please specify a greater value");
jQuery.validator.addClassRules("dept_date", {
greaterrule: ".dept_date"
});
$("form").validate();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.14.0/jquery.validate.min.js"></script>
<form method="post">
First Date
<input type="text" value="01/21/2016" name="first_date" class="dept_date">
<br>Second Date
<input type="text" value="01/22/2016" name="second_date" class="dept_date">
<br>Third Date
<input type="text" value="01/23/2016" name="third_date" class="dept_date">
<br>Fourth Date
<input type="text" value="01/24/2016" name="fourth_date" class="dept_date">
<br>
<input type="submit" value="submit" />
</form>
Related
I'm trying to collect 2 dates using input boxes ids From and To. I would like to subtract 1 day from the date value received using "FROM" text box and pass that new value to the button click event.
The code below works to push values received from the input box. However, i wish to subtract 1 day and pass on the same.
<script src="https://code.jquery.com/jquery-3.4.1.min.js" type="text/javascript"></script>
From : <input type="date" class="form-control" id="From" placeholder="From" aria-label="From">
To : <input type="date" class="form-control" id="To" placeholder="To" aria-label="To">
<input type="button" onclick="location.href='#SITE#/TDL-Test-code.aspx?FromTo=(Date ge datetime%27'+FD+'T00:00:00.000Z%27) and (Date le datetime%27'+ document.getElementById('To').value+'T23:59:00.000Z%27)';" value="Submit" />
Edit:
Following is my moment JS function
var oldfrom = document.getElementById('From').value;
newdate = moment(oldfrom).subtract(1, 'days').format("YYYY-MM-DD");
var FD = newdate;
Issue was fixed with solution below.
document.getElementById("From_Local").oninput = function() {
var dateLocal = document.getElementById("From_Local").value;
var dateUTC = moment.utc(dateLocal).subtract(4, 'hours').format("YYYY-MM-DD");
document.getElementById("From_UTC").value = dateUTC;
}
From:<input name="From" required type="date" id="From_Local">
<input type="hidden" id="From_UTC">
To:<input name="To" required type="date" id="To">
<input type="button" onclick="location.href='SITE_URL?FromTo=(Date%20ge%20datetime%27'+ document.getElementById('From_UTC').value+'T00:00:00.000Z%27)%20and%20(Date%20le%20datetime%27'+ document.getElementById('To').value+'T00:00:00.000Z%27)';" value="Submit" />
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);
})
I want to develop a form that has three input field of which third input with name schoolname should only be visible if age below 15 otherwise it should be hidden. when user enters age below 15 third field should pop up and should be required field. I developed form but when I enter age above 15 the form is not submitting as i set school field as hidden. what should i do to get form submitted if age is above 15 and a mendatory school name field i fage below 15
$("#submit").click(function() {
var age = $("#submit").val();
if (age < 15) {
$("#schoolname").show();
}
});
<form>
<input id="name" type="text" name="name" placeholder="Enter your name" required />
<input id="age" type="number" name="age" placeholder="age" required />
<input id="schoolname" type="text" name="schoolname" placeholder="Enter your school name" style="display:none" required />
<input id="submit" type="submit" name="submit" value="submit">
</form>
You can use keyup event
$("#age").keyup(function() {
var age = $("#age").val();
if (age < 15 && age > 0) {
$("#schoolname").show();
} else {
$("#schoolname").hide();
}
});
var age = $("#submit").val();
change to
var age = $("#age").val();
#age mean that you are getting element by id, and that id is age which you needed in the form
Your age has wrong input value which is string that's why it will be fail. You need change
var age = $("#submit").val();
to
var age = parseInt($("#age").val());
parseInt() will convert the string to integer which faster and better way for if statement.
$("#age").on("change",function() {
var age = $("#age").val();
if (age < 15) {
$('<input id="schoolname" type="text" name="schoolname" placeholder="Enter your school name" required />').insertBefore("#submit");
}else{
$("#schoolname").remove();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
<input id="name" type="text" name="name" placeholder="Enter your name" required />
<input id="age" type="number" name="age" placeholder="age" required />
<input id="submit" type="submit" name="submit" value="submit">
</form>
I want to know how to calculate the input values of the following fields
<input type="text" name="score1" class="auto-sum">
<input type="text" name="score2" class="auto-sum">
<input type="text" name="score3" class="auto-sum">
And update the span text and the hidden field value
TOTAL <span id="amountTotal">0 </span>
<input id="total" type="hidden" name="total" value="">
Thanks in advance
This should do what you want to achieve:
function displaySum () {
var sum = 0;
// iterate over the input fields
$('.auto-sum').each(function () {
var value = $(this).val() || 0;
var number = parseInt(value, 10); // parse the input value to an integer
if (isNaN(number)) {
number = 0;
}
sum += number; // add the value to the sum
});
// set the value of the two elements
$('#amountTotal').text(sum);
$('#total').val(sum);
}
// whenever the value of one of the input fields changes, call the displaySum function
$('.auto-sum').on('change, keyup', displaySum);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="score1" class="auto-sum">
<input type="text" name="score2" class="auto-sum">
<input type="text" name="score3" class="auto-sum">
TOTAL <span id="amountTotal">0 </span>
<input id="total" type="hidden" name="total" value="">
I tried with blur method for after mouseleave from input and check all input values and added ! Then update to wanted element !!
$("input").on("blur",function(){
var total = 0;
$("[type='text']").each(function(){
if($(this).val() != "") {
total += parseInt($(this).val());
}
});
$("#amountTotal").text(total);
$("#total").val(total);
});
Try with query each() function .Use with parseFloat convert the string to number .for real time update do with keyup event
$('.auto-sum').keyup(function(){
var a=0;
$('.auto-sum').each(function(){
a+=parseFloat($(this).val())
})
$('#amountTotal').text(a|0)
$('#total').val(a|0)
console.log($('#total')[0].outerHTML)//its show the hidden field value
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="score1" class="auto-sum" value="2" >
<input type="text" name="score2" class="auto-sum" value="5">
<input type="text" name="score3" class="auto-sum" value="5">
TOTAL <span id="amountTotal" >0 </span>
<input id="total" type="hidden" name="total" value="">
You can use <input type="number" /> inputs to force values entered to be a number.
From there you can call jQuery with the query selector input.auto-sum to get all of the input elements with the class auto-sum
Using the each function you can add the numeric to an accumulator to get the total.
You can then use jQuery with the query selectors needed to set any other elements on the page with the Total value.
function getTotal() {
var total=0;
$('input.auto-sum').each(function(i,e)
{
total += Number($(e).val());
});
$('#amountTotal').text(total);
$('#total').val(total);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Score 1: <input type="number" name="score1" class="auto-sum"><br />
Score 2: <input type="number" name="score2" class="auto-sum"><br />
Score 3: <input type="number" name="score3" class="auto-sum"><br />
TOTAL: <span id="amountTotal">0 </span><br />
<input id="total" type="hidden" name="total" value="">
<input type="button" id="btnTotal" onclick="getTotal()" value="Get Total" />
Please try this one.
$('.auto-sum').on('change',function(){
var totalvalue=0;
$('.auto-sum').each(function(a,b){
totalvalue=totalvalue+parseInt(($(b).val()=='' || isNaN($(b).val()))?0:$(b).val());
});
$('#amountTotal').text(totalvalue);
$('#total').val(totalvalue);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="score1" class="auto-sum">
<input type="text" name="score2" class="auto-sum">
<input type="text" name="score3" class="auto-sum">
TOTAL <span id="amountTotal">0</span>
<input id="total" type="hidden" name="total" value="">
<input type="text" name="score1" class="auto-sum">
<input type="text" name="score2" class="auto-sum">
<input type="text" name="score3" class="auto-sum">
<input type="button" id="calculator" value="Caculate">
$("#calculator").on("click",function (){
var result=0;
var sum_fields=$(".auto-sum");
for(var i=0;i<sum_fields.length;i++){
if(!isNaN(sum_fields[i]) && sum_fields[i]>0){
result += sum_fields[i];
}
}
$("#amountTotal").text(result);
$("#total").val(result);
});
Create array for the fields and loop through them
Check if it not a NaN and more than 0 add them to the result then after the loop attach the result to the hidden field and as a span text
I post this as an answer because the comment areas are to small
At first i think it´s an good idea to write
<input type = "number">
when you don´t want to display the small arrows look at this solution:
https://css-tricks.com/snippets/css/turn-off-number-input-spinners/
the keyup event ist the best way to "live" display the total sum
At last take a look at vue.js
This is the best solution for this scenario.
if you're interested in a pure javascript solution:
All inputs have an oninput event attribute that fires when the input is changed.
You can attach an event handler to the event like so:
<input oninput="updateTotal" />
where updateTotal is a function declared in your script:
function updateTotal { /* code that updates the total */ }
OR if you select the input in your script then you can attach the event handler like this:
selectedInput.oninput = function updateTotal () {
...
}
Here's my implementation that selects all the inputs with the class "auto-sum", assigns the event handler to each of them and updates the code by looping over each input's value attribute:
https://jsfiddle.net/billy_reilly/5dd24v81/2/
function displaySum () {
var sum = 0;
// iterate over the input fields
$('.auto-sum').each(function () {
var value = $(this).val() || 0;
var number = parseInt(value, 10); // parse the input value to an integer
if (isNaN(number)) {
number = 0;
}
sum += number; // add the value to the sum
});
// set the value of the two elements
$('#amountTotal').text(sum);
$('#total').val(sum);
}
// whenever the value of one of the input fields changes, call the displaySum function
$('.auto-sum').on('change, keyup', displaySum);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" name="score1" class="auto-sum">
<input type="text" name="score2" class="auto-sum">
<input type="text" name="score3" class="auto-sum">
TOTAL <span id="amountTotal">0 </span>
<input id="total" type="hidden" name="total" value="">
What do I need to add for the min restriction to make sure the user-input for check-out date is after the user-input for check-in date?
<form>
Enter Check-In Date:<br>
<input type="date" name="checkin"><br>
Enter Check-Out Date:<br>
<input type="date" name="checkout"><br>
</form>
Can this be done with just HTML5 or does it need to include javascript?
java script need for validation .Compare the check in date is less the check out date
function check() {
if(document.getElementById('checkin').value < document.getElementById('checkout').value)
{ console.log('ok')}
else{
console.log('Not allowed')
}
}
<form>
Enter Check-In Date:<br>
<input type="date" id="checkin" name="checkin"><br>
Enter Check-Out Date:<br>
<input type="date" id="checkout" name="checkout" onchange="check()"><br>
</form>
You can use min and max attributes of HTML5 input date
<form>
Enter Check-In Date:<br>
<input type="date" name="checkin" max="2016-11-06"><br>
Enter Check-Out Date:<br>
<input type="date" name="checkout" min="2016-11-07"><br>
</form>
Where date of max in "checkin" is smaller than date of min in "checkout".
Or try validation in javascript.
document.getElementById("2").disabled = true;
$("#1").on("change paste keyup", function() {
console.log($(this).val());
var testDate= $(this).val();
var date_regex = /^\d{4}-\d{2}-\d{2}$/;
if((date_regex.test(testDate)))
document.getElementById("2").disabled = false;
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form>
Enter Check-In Date:<br>
<input type="date" name="checkin" id="1"><br>
Enter Check-Out Date:<br>
<input type="date" name="checkout" id="2"><br>
</form>