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>
Related
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();
}
}
});
})
i am newer in jquery And I have a problem in Multiply with jquery
my sample Form is :
The first line of the form is calculated correctly But multiplication is not done from the second line.
MyCode is :
$(document).ready(function () {
var rowIdx = 0;
$('#addBtn').on('click', function () {
$('#tbody').append(`<tr id="R${++rowIdx}">
<td>${rowIdx}</td>
<td>
<select id="group" name="group" class="form-control form-select">
#foreach($groups as $group)
<option>{{ $group->title }}</option>
#endforeach
</select>
</td>
<td><input type="number" name="number" id="a1"></td>
<td><input type="number" name="price" id="a2"></td>
<td><input type="number" name="total_price" id="a3"></td>
<td class="text-center">
<button class="btn btn-danger remove"
type="button">Remove</button>
</td>
</tr>`);
$('#a1').keyup(calculate);
$('#a2').keyup(calculate);
function calculate(e)
{
$('#a3').val($('#a1').val() * $('#a2').val());
}
});
$('#tbody').on('click', '.remove', function () {
var child = $(this).closest('tr').nextAll();
child.each(function () {
var id = $(this).attr('id');
var idx = $(this).children('.row-index').children('p');
var dig = parseInt(id.substring(1));
idx.html(`Row ${dig - 1}`);
$(this).attr('id', `${dig - 1}`);
});
$(this).closest('tr').remove();
rowIdx--;
});
});
You can use $(this).closest("tr") to get closest tr where keyup/change has been taken place then using same get required input values and add total to your total_price inputs .
Demo Code :
$(document).ready(function() {
var rowIdx = 0;
$('#addBtn').on('click', function() {
$('#tbody').append(`<tr id="R${++rowIdx}">
<td>${rowIdx}</td>
<td>
<select id="group" name="group" class="form-control form-select">
#foreach($groups as $group)
<option>{{ $group->title }}</option>
#endforeach
</select>
</td>
<td><input type="number" min ="0" value="0" name="number"></td>
<td><input type="number" min ="0" value="0" name="price"></td>
<td><input type="number"min ="0" value="0" name="total_price"></td>
<td class="text-center">
<button class="btn btn-danger remove"
type="button">Remove</button>
</td>
</tr>`);
});
//on key up or change
$(document).on("change keyup", "tbody input[type=number]", function() {
var qty = 0,
total = 0;
//get closest tr
var selector = $(this).closest("tr")
//get numer & price from same row
var number = parseInt(selector.find("[name=number]").val())
var price = parseInt(selector.find("[name=price]").val())
//add total in totalprice in same row
selector.find('[name=total_price]').val(number * price);
//loop thorugh each trs
$("#tbody tr").each(function() {
//add value of each inputs
qty += parseInt($(this).find("[name=number]").val())
total += parseInt($(this).find("[name=total_price]").val())
})
//add result in dom..
$("#qty").text(qty);
$("#total").text(total);
})
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table>
<tbody id="tbody"></tbody>
</table>
<button id="addBtn" type="button">Add</button> <br/>Total qty : <span id="qty"></span> <br/>Total price : <span id="total"></span>
Here is a version of selector that might work for you.
let parentTR = $(this).parents('tr')[0];
let resultInput = $(parentTR).find("#a3");
resultInput.val($(parentTR).find("#a1").val() * $(parentTR).find("#a2").val());
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>
<input type="text" class="form-control name" name="name" id="focus_me" required maxlength="50" letters-with-space="" ng-trim="false" tabindex="1" ng-model="vm.detail.name" ng-paste="paste($event.originalEvent)" ng-init="vm.detail.name = null">
$scope.paste = function (event,field) {
var item = event.clipboardData.items[0];
item.getAsString(function (data) {
$scope.pastedData = data;
$scope.$apply();
});
}
Input : 继续
here is input , i am not able to paste it into textbox. how to enable it?
checkout this https://jsfiddle.net/geekcode/s91t2ryg/11/
I'm able to paste the Chinese content, just pass $event instead of $event.originalEvent.
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="ctrl">
<input ng-model="mod" type="text" ng-paste="paste($event)" ng-init="mod = null">
{{mod}}
<input ng-model="mod1" type="text">
</div>
</div>
<script>
var app = angular.module("app", []);
app.controller("ctrl", function($scope){
$scope.paste = function (event,field) {
var item = event.clipboardData.items[0];
item.getAsString(function (data) {
$scope.pastedData = data;
$scope.$apply();
});
}
});
</script>
I am having three checkboxes as below:
<input type="checkbox" ng-true-value="Orange" ng-model="selectMe1">Orange
<input type="checkbox" ng-true-value="Pink" ng-model="selectMe2">Pink
and one more checkbox to clear all checkboxes:
<input type="checkbox" ng-true-value="none" ng-model="clearMe">None
Thank you in advance..
<input type="checkbox" ng-true-value="none" ng-click="clearAll()" ng-model="clearMe">
Javascript code
$scope.clearAll=function(){
$scope.selectMe1=false;
$scope.selectMe2=false;
}
These links might be helpfull for you:
http://www.w3schools.com/angular/ng_ng-checked.asp
http://www.w3schools.com/angular/tryit.asp?filename=try_ng_ng-checked
A toggle function in ng-click should help you. Try this.
<input type="checkbox" ng-true-value="0" ng-model="clearMe" ng-click="toggle()">None
In your controller, you can add
$scope.toggle = function()
{
$scope.selectMe1 = 'false';
$scope.selectMe2 = 'false';
}
this is a sample that can help you :
Html
<div>
<ul ng-controller="checkboxController">
<li>Check All
<input type="checkbox" ng-model="selectedAll" ng-click="checkAll()" />
</li>
<li ng-repeat="item in Items">
<label>{{item.Name}}
<input type="checkbox" ng-model="item.Selected" />
</label>
</li>
</ul>
</div>
Javascript
angular.module("CheckAllModule", [])
.controller("checkboxController", function checkboxController($scope) {
$scope.Items = [{
Name: "Item one"
}, {
Name: "Item two"
}, {
Name: "Item three"
}];
$scope.checkAll = function () {
if ($scope.selectedAll) {
$scope.selectedAll = true;
} else {
$scope.selectedAll = false;
}
angular.forEach($scope.Items, function (item) {
item.Selected = $scope.selectedAll;
});
};
});
this a working demo
Add this on your last checkbox ng-change=disableall() into your last checkbox,
on your controller:
$scope.selectMe1 = false;//init
$scope.selectMe2 = false;//init
$scope.clearMe = false;//init
$scope.disableall() = function(){
//let's say that you clear when the checkbox is true
if($scope.clearMe){
$scope.selectMe1 = false;
$scope.selectMe2 = false;
}
}
Hope helps ,good luck.
try this,
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"> </script>
<div ng-app="app" ng-controller="ctrl">
<form id="selectionForm">
<input type="checkbox" ng-click="toggleAll()" >Select all
<br>
<div ng-repeat = "option in options">
<input type="checkbox" ng-model="option.selected" ng-change="optionToggled()">{{option.value}}
</div>
</form>
{{options}}
</div>
<script>
angular.module("app", []).controller("ctrl", function($scope){
$scope.options = [
{value:'Option1', selected:true},
{value:'Option2', selected:false}
];
$scope.toggleAll = function() {
var toggleStatus = false;
angular.forEach($scope.options, function(itm){ itm.selected = toggleStatus; });
}
});
</script>
Simple Solution:
<input type="checkbox" ng-model="selectMe1">Orange
<input type="checkbox" ng-model="selectMe2">Pink
<input type="checkbox" ng-model="clearMe" ng-click="selectMe1=selectMe2=0;">None