[英]How to restrict input field value to certain number
I have an HTML table inside which i have a column AcceptedQty
which is input field 我有一个HTML表,其中有一个
AcceptedQty
列,它是输入字段
Total i have 5 columns Code
, Item Name
, unitcode
, Quantity
and AcceptedQty
two of them Quantity
and AcceptedQty
have same values but AcceptedQty
is input field so user can input any number inside that and i have made that type="number" to enter only numbers 我总共有5列
Code
, Item Name
, unitcode
, Quantity
和AcceptedQty
他们两个Quantity
和AcceptedQty
有相同的价值观,但AcceptedQty
是那么用户可以输入输入字段中的任何数量的内部的,我已经提出,类型=“数字”只输入数字
What i am trying to do 我想做什么
code
1326 Quantity
is 3 so while editing AcceptedQty
i want to restrict user not to enter any number greater then 3 code
1326的Quantity
为3,所以在编辑AcceptedQty
我要限制用户不要输入大于3的任何数字 Snippet 片段
var tableDataDraft = [{ "Code": "1326", "Item Name": "PINEAPPLE KG", "unitcode": "NOS", "Quantity": "3.0000", "AcceptedQty": "3.0000" }, { "Code": "1494", "Item Name": "2D CAKE CHARGES PER KG", "unitcode": "NOS", "Quantity": "3.0000", "AcceptedQty": "3.0000" } ] function addTableDraft(tableDataDraft) { var col = Object.keys(tableDataDraft[0]); var countNum = col.filter(i => !isNaN(i)).length; var num = col.splice(0, countNum); col = col.concat(num); var table = document.createElement("table"); var tr = table.insertRow(-1); for (var i = 0; i < col.length; i++) { var th = document.createElement("th"); th.innerHTML = col[i]; tr.appendChild(th); tr.classList.add("text-center"); tr.classList.add("head") } for (var i = 0; i < tableDataDraft.length; i++) { tr = table.insertRow(-1); for (var j = 0; j < col.length; j++) { let tabCell = tr.insertCell(-1); var hiddenField = document.createElement("input"); //creating input field hidden hiddenField.style.display = "none"; var tabledata = tableDataDraft[i][col[j]]; if (tableDataDraft[i]['Code'] === tableDataDraft[i][col[j]]) { //now setting html attributes tabCell.innerHTML = tabledata; hiddenField.setAttribute('name', 'Item_Code'); hiddenField.setAttribute('value', tabledata); tabCell.appendChild(hiddenField); } if (tableDataDraft[i]['Item Name'] === tableDataDraft[i][col[j]]) { tabCell.innerHTML = tabledata; hiddenField.setAttribute('name', 'Item_Name'); hiddenField.setAttribute('value', tabledata); tabCell.appendChild(hiddenField); } if (tableDataDraft[i]['unitcode'] === tableDataDraft[i][col[j]]) { tabCell.innerHTML = tabledata; hiddenField.setAttribute('name', 'Unit_code'); hiddenField.setAttribute('value', tabledata); tabCell.appendChild(hiddenField); } if (col[j] === 'Quantity') { tabCell.innerHTML = tabledata; hiddenField.setAttribute('name', 'Quantity'); tabCell.appendChild(hiddenField); } if (col[j] === 'AcceptedQty') { var quantityField = document.createElement("input"); quantityField.style.border = "none"; quantityField.style["text-align"] = "right"; quantityField.setAttribute("name", "AcceptedQty"); quantityField.setAttribute("autocomplete", "on"); quantityField.setAttribute("value", tabledata); quantityField.setAttribute("type", "number"); quantityField.setAttribute("required", "required"); quantityField.classList.add("dataReset"); quantityField.toLocaleString('en-IN'); tabCell.appendChild(quantityField); } if (j > 1) tabCell.classList.add("text-right"); } } var divContainer = document.getElementById("table"); divContainer.innerHTML = ""; divContainer.appendChild(table); table.classList.add("table"); table.classList.add("table-striped"); table.classList.add("table-bordered"); table.classList.add("table-hover"); } addTableDraft(tableDataDraft)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css"> <div class="table-responsive" id="commonDvScroll"> <table id=table></table> </div>
and i have made that type="tel" to enter only numbers
而且我已经使type =“ tel”仅输入数字
Use type="number"
( "tel"
is a telephone number ) and the min
and max
attributes (and reflected properties) of the HTMLInputElement
(and step
if you don't want fractional values). 使用
type="number"
( "tel"
是电话号码 )和HTMLInputElement
的min
和max
属性(以及反射的属性)(如果您不希望使用小数,则使用step
)。 Probably also include an input
handler to handle browsers without HTML5 field features. 可能还包括一个
input
处理程序,用于处理没有HTML5字段功能的浏览器。
See the ***
commented lines: 请参阅
***
注释行:
var tableDataDraft = [{ "Code": "1326", "Item Name": "PINEAPPLE KG", "unitcode": "NOS", "Quantity": "3.0000", "AcceptedQty": "3.0000" }, { "Code": "1494", "Item Name": "2D CAKE CHARGES PER KG", "unitcode": "NOS", "Quantity": "3.0000", "AcceptedQty": "3.0000" } ] function addTableDraft(tableDataDraft) { var col = Object.keys(tableDataDraft[0]); var countNum = col.filter(i => !isNaN(i)).length; var num = col.splice(0, countNum); col = col.concat(num); var table = document.createElement("table"); var tr = table.insertRow(-1); for (var i = 0; i < col.length; i++) { var th = document.createElement("th"); th.innerHTML = col[i]; tr.appendChild(th); tr.classList.add("text-center"); tr.classList.add("head") } for (var i = 0; i < tableDataDraft.length; i++) { tr = table.insertRow(-1); for (var j = 0; j < col.length; j++) { let tabCell = tr.insertCell(-1); var hiddenField = document.createElement("input"); //creating input field hidden hiddenField.style.display = "none"; var tablerow = tableDataDraft[i]; // *** var tabledata = tablerow[col[j]]; // *** if (tableDataDraft[i]['Code'] === tableDataDraft[i][col[j]]) { //now setting html attributes tabCell.innerHTML = tabledata; hiddenField.setAttribute('name', 'Item_Code'); hiddenField.setAttribute('value', tabledata); tabCell.appendChild(hiddenField); } if (tableDataDraft[i]['Item Name'] === tableDataDraft[i][col[j]]) { tabCell.innerHTML = tabledata; hiddenField.setAttribute('name', 'Item_Name'); hiddenField.setAttribute('value', tabledata); tabCell.appendChild(hiddenField); } if (tableDataDraft[i]['unitcode'] === tableDataDraft[i][col[j]]) { tabCell.innerHTML = tabledata; hiddenField.setAttribute('name', 'Unit_code'); hiddenField.setAttribute('value', tabledata); tabCell.appendChild(hiddenField); } if (col[j] === 'Quantity') { tabCell.innerHTML = tabledata; hiddenField.setAttribute('name', 'Quantity'); tabCell.appendChild(hiddenField); } if (col[j] === 'AcceptedQty') { var quantityField = document.createElement("input"); quantityField.style.border = "none"; quantityField.style["text-align"] = "right"; quantityField.setAttribute("name", "AcceptedQty"); quantityField.setAttribute("autocomplete", "on"); quantityField.setAttribute("value", tabledata); quantityField.setAttribute("type", "number"); quantityField.min = 0; // *** quantityField.max = tablerow.Quantity; // *** quantityField.setAttribute("required", "required"); quantityField.classList.add("dataReset"); quantityField.toLocaleString('en-IN'); tabCell.appendChild(quantityField); } if (j > 1) tabCell.classList.add("text-right"); } } var divContainer = document.getElementById("table"); divContainer.innerHTML = ""; divContainer.appendChild(table); table.classList.add("table"); table.classList.add("table-striped"); table.classList.add("table-bordered"); table.classList.add("table-hover"); } addTableDraft(tableDataDraft)
input:invalid { color: #d00; border: 1px solid #d00 !important; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css"> <div class="table-responsive" id="commonDvScroll"> <table id=table></table> </div>
Note that the user will still be able to enter a higher number, but the form won't validate. 请注意,用户仍将能够输入更大的数字,但是该表格将无法通过验证。 See this tutorial on MDN.
请参阅有关MDN的本教程 。
As @TJ Crowder mentioned, make use of the input type number
如@TJ Crowder所述,利用输入类型
number
That input type allows you to set a max
value, which is based of the previous columns value 该输入类型允许您设置一个
max
,该max
基于先前的列值
Additionally, you'll want to add an event listener to the input to "listen" / detect changes to the input, so that you can act upon any changes, restricting the input value as you see fit 此外,您需要向输入添加事件侦听器,以“侦听” /检测输入的更改,以便您可以对任何更改进行操作,并根据需要限制输入值
var tableDataDraft = [{ "Code": "1326", "Item Name": "PINEAPPLE KG", "unitcode": "NOS", "Quantity": "3.0000", "AcceptedQty": "3.0000" }, { "Code": "1494", "Item Name": "2D CAKE CHARGES PER KG", "unitcode": "NOS", "Quantity": "3.0000", "AcceptedQty": "3.0000" } ] function addTableDraft(tableDataDraft) { var col = Object.keys(tableDataDraft[0]); var countNum = col.filter(i => !isNaN(i)).length; var num = col.splice(0, countNum); col = col.concat(num); var table = document.createElement("table"); var tr = table.insertRow(-1); for (var i = 0; i < col.length; i++) { var th = document.createElement("th"); th.innerHTML = col[i]; tr.appendChild(th); tr.classList.add("text-center"); tr.classList.add("head") } for (var i = 0; i < tableDataDraft.length; i++) { tr = table.insertRow(-1); for (var j = 0; j < col.length; j++) { let tabCell = tr.insertCell(-1); var hiddenField = document.createElement("input"); //creating input field hidden hiddenField.style.display = "none"; var tabledata = tableDataDraft[i][col[j]]; if (tableDataDraft[i]['Code'] === tableDataDraft[i][col[j]]) { //now setting html attributes tabCell.innerHTML = tabledata; hiddenField.setAttribute('name', 'Item_Code'); hiddenField.setAttribute('value', tabledata); tabCell.appendChild(hiddenField); } if (tableDataDraft[i]['Item Name'] === tableDataDraft[i][col[j]]) { tabCell.innerHTML = tabledata; hiddenField.setAttribute('name', 'Item_Name'); hiddenField.setAttribute('value', tabledata); tabCell.appendChild(hiddenField); } if (tableDataDraft[i]['unitcode'] === tableDataDraft[i][col[j]]) { tabCell.innerHTML = tabledata; hiddenField.setAttribute('name', 'Unit_code'); hiddenField.setAttribute('value', tabledata); tabCell.appendChild(hiddenField); } if (col[j] === 'Quantity') { tabCell.innerHTML = tabledata; hiddenField.setAttribute('name', 'Quantity'); tabCell.appendChild(hiddenField); } if (col[j] === 'AcceptedQty') { var quantityField = document.createElement("input"); quantityField.style.border = "none"; quantityField.style["text-align"] = "right"; quantityField.setAttribute("name", "AcceptedQty"); quantityField.setAttribute("autocomplete", "on"); quantityField.setAttribute("value", tabledata); quantityField.setAttribute("type", "number"); quantityField.setAttribute("required", "required"); quantityField.setAttribute("max", parseInt(tableDataDraft[i]["Quantity"])); quantityField.setAttribute("min", 0); quantityField.setAttribute("step", 0.1); quantityField.addEventListener("change", function() { let max = parseFloat(this.getAttribute('max')); let min = parseFloat(this.getAttribute('min')); let val = parseFloat(this.value) val = val > max ? max : val; val = val < min ? min : val; if (val != this.value) { alert("Input is incorrect"); this.focus(); } this.value = val; }); quantityField.setAttribute("min", 0); quantityField.classList.add("dataReset"); quantityField.toLocaleString('en-IN'); tabCell.appendChild(quantityField); } if (j > 1) tabCell.classList.add("text-right"); } } var divContainer = document.getElementById("table"); divContainer.innerHTML = ""; divContainer.appendChild(table); table.classList.add("table"); table.classList.add("table-striped"); table.classList.add("table-bordered"); table.classList.add("table-hover"); } addTableDraft(tableDataDraft)
input:invalid { outline: 1px solid red; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.2/css/bootstrap.min.css"> <div class="table-responsive" id="commonDvScroll"> <table id=table></table> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.