How to disable/enable keys

Viewed 74

We would like to learn how to disable/enable keys for within a text box, with coding as simple as possible.

For example in a text box that provides a decimal measurement we would only want numbers 0-9 & period (.) & backspace to be enabled & all other keys disabled.

For simplicity is it possible to disable all keys then enable (0-9 . backspace) rather than individually disable all the others?

Another example would be to enable only a-z lower case.

Or A-Z upper case & maybe some others like @, #, $, %, &.

Another idea we have is to disable these characters < > { } [ ] from all text boxes & text areas, and also record the users ID, together with a warning message, of whoever attempts to insert those characters. We are developing in CodeIgniter3. Probably appropriate to facilitate backend with form_validation using a regex. We are still working on that.

We are familiar with the Javascript Char Codes (Key Codes) but wonder if the numbers are consistent. https://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

I tried this coding but it didnt work, however we would like something similar;

<script>
function accept_numeric_only(e) {
e = e || window.event;
var k = e.keyCode || e.which;
var upperlowercase = e.shiftKey;
if(uppercase && lowercase && k <= 7 && k >= 9 && k <= 47 && k >= 58 && k <= 189 && k >= 191) {
document.getElementById("error").style.display = "inline";
return false; }
document.getElementById("error").style.display = "none";
return true; }
function init() {
document.getElementById("length, width, height, diameter, weight, quantity, price").onkeydown = accept_numeric_only; } //These are the text boxes to be disabled
</script>

Please dont tell me to try HTML coz that doesnt work properly.

We are developing in CodeIgniter3 and considering form_validation as well as frontend Javascript, but the 2nd part of the following form_validation does not work.

$config = array(
array('field' => 'length', 'label' => 'Length', 'rules' => 'required|regex_match/[0-9.]/'', 'errors' => array( 'required' => 'Provide%s', 'The %s field must only contain  numbers')));
$this->form_validation->set_rules($config);
0 Answers
Related