I am losing my mind over this simple part. I have a table with over 20 columns, where some of them are editable some are not.
What I need now is that when a user adds a new row he is forced to write only numbers in a few cells. So far I tried examples from here :
accept only numbers in td
accept numbers using isNumeric()
both of them didn't work
It only worked if I called the functions on my whole table. But that is not usable since there are also cells that should also accept String values.
$("#my_id").keydown(function(e) {
if ($.inArray(e.keyCode, [46, 8, 9, 27, 13, 110]) !== -1 ||
((e.keyCode == 65 || e.keyCode == 86 || e.keyCode == 67) && (e.ctrlKey === true || e.metaKey === true)) ||
(e.keyCode >= 35 && e.keyCode <= 40)) {
return;
}
if ((e.shiftKey || (e.keyCode < 48 || e.keyCode > 57)) && (e.keyCode < 96 || e.keyCode > 105)) {
e.preventDefault();
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<table id="my_id" border="1" width="100px" height="50px">
<thead></thead>
<tbody>
<tr>
<th>1</th>
<td class="allow_only_numbers" contenteditable="true" id="idid"></td>
</tr>
</tbody>
</table>
This particular code will not work here (on Stackoverflow) because the script from the first link is not included: (but will if I add it to my existing code)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
But if I insert the script part in my code there is a problem with the date picker then... and I can't use it... My guess is that it is probably something simple to correct but I can't figure it out -.-
UPDATE I managed to correct it. I need the "only numeric values" when a new row was added, before that it is useless. So I added the function inside the "add row"- function and now it works. Thank you all for help