JQuery autosubmit - html

I have a form with three input elements. I want to run my validate function when the last input element is not equal to null. Any ideas please. Here's the code :
$(document).ready(function () {
$("#number input").keyup(function () {
if (this.value.length == this.maxLength) {
$(this).next('#number input').focus();
}
});
})
Here's the HTML :
<div id="number">
<form action="" id="myform">
<input placeholder="hr" maxlength="2" type="number" id="uHrs" pattern="[1-12]" autofocus></input>
<input placeholder="min" maxlength="2" type="number" id="uMins" pattern="[0-59]"></input>
<input placeholder="sec" maxlength="2" type="number" id="uSecs" pattern="[0=50]"></input>
<button type="submit" id="subtime" value="submit" onclick="checkInput()">Submit</button>
</form>

You can try filter, also I use the input event so we can paste. We could even split a too long number and spread it to the next fields if we want to support pasting the complete set of values
Full solution: https://jsfiddle.net/mplungjan/3mbqw80h/
$(function() {
const $inputs = $("#number input"),
$form = $("#number");
$inputs.on("input", function() {
if (this.value.length == this.maxLength) {
const $next = $(this).next('#number input');
if ($next.length) $next.select().focus();
if ($inputs.filter(function() {
return this.value.trim() !== ""
}).length === $inputs.length) {
checkInput();
}
}
});
})

Related

assign value from two input to a single input type hidden

I am trying to get the value of first input or second input and assign it to a hidden input type. When the value updates from the above default value.
HTML
<input id="amount2" name="s_value6" type="number" value="48">
<input id="amount3" name="s_value11" type="number" value="500">
<input id="mak" type ="hidden" name="distance" value="">
jQuery
if(mafsForm.find("#amount3").val().length !== 0 || mafsForm.find("#amount2").val().length !== 0) {
var s_value11 = mafsForm.find("#amount3").val();
var s_value6 = mafsForm.find("#amount2").val();
var dist = $('#mak').val($(this).val());
}
Hope this is what you are looking for. To test this, remove 'hidden' from the hidden input field. Then change the value of any of the first two input boxes. On every change, 3rd input box will be updated with latest value. Below code only shows latest value from the last updated input box
$("#amount2, #amount3").change(function(){
var changedVal = $(this).val();
if(changedVal !== '0'){
$("#mak").val(changedVal);
}
});
This is a working example of pure javascript ...
<input id="amount2" name="s_value6" type="number" value="48">
<input id="amount3" name="s_value11" type="number" value="500">
<input id="mak" type="hidden" name="distance" value="">
<script>
var amnt2 = document.getElementById("amount2");
var amnt3 = document.getElementById("amount3");
var mak = document.getElementById("mak");
amnt2.addEventListener("change", function(event) {
setValue(this.value);
});
amnt3.addEventListener("change", function(event) {
setValue(this.value);
});
function setValue(num) {
mak.value = num;
}
</script>
The mafsForm variable was not assigned a selector in your code, so instead of mafsForm.find(selector).val() I wrote $(selector).val(). Also, I used the parseInt() method to convert the value to a number for summation. You can easily adapt this code for yourself.
$('#amount2, #amount3').on('change', function() {
var s_value11 = $("#amount3").val();
var s_value6 = $("#amount2").val();
if(s_value11.length !== 0 || s_value6.length !== 0) {
value_sum = parseInt(s_value11) + parseInt(s_value6);
console.log(value_sum);
var dist = $('#mak').val(value_sum);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="amount2" name="s_value6" type="number" value="48">
<input id="amount3" name="s_value11" type="number" value="500">
<input id="mak" type ="hidden" name="distance" value="">
Your issue is that $(this) is referencing the window object. Here's a working solution:
var mafsForm = $('#mafsForm');
var dist;
if(mafsForm.find("#amount3").val().length !== 0 || mafsForm.find("#amount2").val().length !== 0) {
var s_value11 = mafsForm.find("#amount3").val();
var s_value6 = mafsForm.find("#amount2").val();
dist = $('#mak').val(s_value11);
};
console.log(dist.val());
// Now if you want to update the value of the hidden value when changing the value of a filed you can do this
mafsForm.find("#amount3").on('input', function() {
var s_value11 = mafsForm.find("#amount3").val();
dist = $('#mak').val(s_value11);
console.log(dist.val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<form id = "mafsForm">
<input id="amount2" name="s_value6" type="number" value="48">
<input id="amount3" name="s_value11" type="number" value="500">
<input id="mak" type ="hidden" name="distance" value="">
</form>

Placeholder doesn't show

Guys I've read everything here about that topic already, but I still can't figure out why my Placeholder is not showing up. Hope you can share your expertise and tell what I'm doing wrong.
<input type="text" name="search" class="input-block-level search-query" placeholder="Enter your name" required="">
It's just not showing up in any browser. Thanks is advance!
You can try use this code to your placeholder show on this input.
$(document).ready(function() {
function add() {if($(this).val() == ''){$(this).val($(this).attr('placeholder')).addClass('placeholder');}}
function remove() {if($(this).val() == $(this).attr('placeholder')){$(this).val('').removeClass('placeholder');}}
if (!('placeholder' in $('<input>')[0])) {
$('input[placeholder]').blur(add).focus(remove).each(add);
$('div').submit(function(){$(this).find('input[placeholder]').each(remove);});
}
});
<div>
<input type="text" name="search" class="input-block-level search-query" placeholder="Enter your name" required>
</div>
I found this link which will give us a good idea about how we fix placeholder problem it will use jquery and it will be a good solution.
http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html
And here is the full jquery code
// Released under MIT license: http://www.opensource.org/licenses/mit-license.php
$('[placeholder]').focus(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
input.removeClass('placeholder'); }
}).blur(function() {
var input = $(this);
if (input.val() == '' || input.val() == input.attr('placeholder')) {
input.addClass('placeholder');
input.val(input.attr('placeholder'));
}
}).blur().parents('form').submit(function() {
$(this).find('[placeholder]').each(function() {
var input = $(this);
if (input.val() == input.attr('placeholder')) {
input.val('');
}
})
});

How to check if the value is present in the array of inputs

Here is my code
i have an input textbox where I get a new value
this line doesn't work,
$(document).on('click', '#af_cbms_add_depID', function()
{
var toclone=$("#newdepID").val();
var torefer=$("#af_cbms_question_item").val();
if((toclone!=='') && (toclone>0))
{
///here's the problem
$(".lahatkami").each(function()
{
input = $(this).val();
//i also tried the other
//if(this.value===toclone)
if(input===toclone)
{
alert("There is a duplicate value " + this.value);
$('#newdepID').val('');
$('#newdepID').focus();
return false;
}
else
{
$("#af_cbms_depIDset").append('<input type="text" name="'+torefer+'" id="depID'+toclone+'" class="lahatkami" value="'+toclone+'" style="width:40px !important; text-align:center;" readonly />');
$('#newdepID').val('');
$('#newdepID').focus();
}
});
}
else
{
alert('No dependent question item was set!');
$('#newdepID').focus();
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="textbox" id="newdepID" value="3"/> sample we a number 3 value
<button id="af_cbms_add_depID">Submit</button> //submits the value
//here are the inputs
<input type="textbox" class="lahatkami" id="newdepID" value="3"/>
<input type="textbox" class="lahatkami" id="newdepID" value="5"/>
<input type="textbox" class="lahatkami" id="newdepID" value="4"/>
after clicking submit i already coded it if the text input is empty.
but inside that if not empty i check again if the value is already in the set of inputs
i put a console.log to check inside but not working
try to this..
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
$( "#af_cbms_add_depID" ).click(function()
{
var toclone= $("#newdepID").val();
var torefer=$("#af_cbms_question_item").val();
if((toclone!=='') && (toclone > 0)) {
$(".lahatkami").each(function() {
input = $(this).val();
//alert(input);
if(input===toclone)
{
alert("There is a duplicate value " + this.value);
$('#newdepID').val('');
$('#newdepID').focus();
return false;
}else {
$("#af_cbms_depIDset").append('<input type="text" name="'+torefer+'" id="depID'+toclone+'" class="lahatkami" value="'+toclone+'" style="width:40px !important; text-align:center;" readonly />');
$('#newdepID').val('');
$('#newdepID').focus();
}
});
} else {
alert('No dependent question item was set!');
$('#newdepID').focus();
}
});
</script>
i am not clear with you exact requirement but i think below is code whic you looking for.
$(document).on('click', '#af_cbms_add_depID', function(){
var toclone=$("#newdepID").val();
var torefer=$("#af_cbms_question_item").val();
$("#af_cbms_depIDset").html("");
if((toclone!=='') && (toclone>0)){
var $tmpdom = $("<span></span>");
$(".lahatkami").each(function(){
input = $(this).val();
if(input===toclone){
alert("There is a duplicate value " + this.value);
$(this).val('');
$(this).focus();
return;
}else {
$tmpdom.append('<input type="text" name="'+torefer+'" id="depID'+toclone+'" class="lahatkami" value="'+toclone+'" style="width:40px !important; text-align:center;" readonly />');
}
});
$("#af_cbms_depIDset").append($tmpdom.html());
}else{
alert('No dependent question item was set!');
$('#newdepID').focus();
}
});

Text field allow only numbers and decimals by user input in angularjs

I have a text field which need to allow only numbers and decimals by user.
And also need to set minimum and maximum value.
<td ng-class="{ 'has-error' : eForm.marks_{{$index}}.$dirty && eForm.marks_{{$index}}.$error.required }">
<input type="text" name="marks_{{$index}}" ng-model="data.marks" placeholder="% of Marks" ng-pattern="/^[0-9]+([,.][0-9]+)?$/" class="form-control" ng-disabled="!eEditMode[$index]" min="1" max="100" ng-required="true">
<span ng-show="eForm.marks_{{$index}}.$dirty && eForm.marks_{{$index}}.$error.required" class="help-block">Marks is required</span>
</td>
You can use the number input:
<input type="number" name="input" ng-model="example.value"
min="0" max="99" required>
For more details: input components in ng / input[number]
This works for me:
Controller :
var app = angular.module('myApp', []);
app.controller('MainCtrl', function($scope) {
});
app.directive("marksRegex", function() {
var regexp;
return {
restrict: "A",
link: function(scope, elem, attrs) {
regexp = /^([0-9]([0-9]([\.]([0-9]([0-9]?)?)?)?)?)?$/;
var char;
elem.bind("keypress", function(event) {
if (event.which == 8) return;
char = String.fromCharCode(event.which);
if (!regexp.test(elem.val() + char))
event.preventDefault();
})
}
}
});
HTML :
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp">
<div ng-controller="MainCtrl">
<label>Marks %:</label>
<input type="text" marks-regex>
</div>
</div>

Getting bootstrap validation to work

I'm using bootstrap for my site and I'm busy creating a form. I have some required fields but if I submit my form without filling those fields I don't get a red box around the input field. But if I click on the input box I get the red box.
Here is my html
<li>
<div class="form-group">
<label>* First Name</label>
<div class="registration_firstName has-error">
<input id="firstName" type="text" name="firstName" class="form-control" required>
</div>
</div>
</li>
<button value="" class="registration_submit">Send</button>
Here is my js
$(".registration_submit").click(function(){
var errorMsg = "";
$(".error-success").hide();
$(".has-error").each(function(index, element){
if($.trim($(this).val()) == ""){
errorMsg+= "error";
}
if($(this).attr("id") == "firstName"){
errorMsg+= "error";
}
});
if(errorMsg != ""){
$(".submit").show();
$(".error-success").html("please fill out the required fields.");
$(".error-success").fadeIn(250);
return false;
}
if($.trim(ajaxemail) == "success"){
$(".submit").show();
$(".error-success").html("Thank you. A confirmation email has been sent.");
$(".error-success").fadeIn(250);
$('#email').val("");
}else{
$(".submit").show();
}
});
Here is a jsfiddle: JSFIDDLE
You can use a bootstrap extension for this:
http://1000hz.github.io/bootstrap-validator/
and use it like this:
$('form').validator().on('submit', function (e) {
if (e.isDefaultPrevented()) {
return false;
} else {
return true;
}
});