How to disable/enable keys |
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 . backspece) 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 familiar with the Javascript Char Codes (Key Codes) but wonder if the numbers are consistent. https://www.cambiaresearch.com/articles/...-key-codes I tried this coding but it didnt work, however we would like someting similar; PHP Code: <script> Please dont tell us to try HTML coz that doesnt work properly. In addition to disabling keys, is it possible to disallow by form_validation? Has anybody any answers or ideas?
Test onkey() event you can controller char is typing
Enlightenment Is Freedom
You can use regex and disable input for those characters you don't allow.
Here is example Code In regex string you can put or remove characters you don't want to allow like in here, allowed characters are: - letters from a-z (lowercase) - letters from A-Z (uppercase) - numbers from 0-9 - space - dot - @ sign - #sign - $ sign - % sign - & sign PHP Code: [a-zA-Z0-9 .@#$%&]
(02-10-2021, 01:33 AM)brianjamestorr Wrote: You can use regex and disable input for those characters you don't allow. Thanks @brianjamestorr but is that Javascript, PHP or other? Can you give an example of some coding?
(02-11-2021, 12:23 PM)christaliise Wrote:(02-10-2021, 01:33 AM)brianjamestorr Wrote: You can use regex and disable input for those characters you don't allow. Well depends what you want to have, block user to enter character when typing or when form is submitted. With javascript you can block user enters letters in input, but this is not really secure, so you can do it in javascript and also in php just to be safe. Using regex with built in function for form validation PHP Code: $this->form_validation->set_rules('', '', 'regex_match[/a-zA-Z0-9 .@#$%&/]'); then message you will get if character is not allowed is "The {field} field is not in correct format.", but you can change that message.
(02-12-2021, 08:26 AM)brianjamestorr Wrote:(02-11-2021, 12:23 PM)christaliise Wrote:(02-10-2021, 01:33 AM)brianjamestorr Wrote: You can use regex and disable input for those characters you don't allow. Thanks @brianjamestorr - I think it is prudent to have both, frontend & backend. In the meantime Im focusing on the form_validation & only one text box (Length). I have added your snippet of code (but allowing only 0-9 .) in addition to an existing form_validation line as below, but I get the following error. Message: Call to undefined function length() PHP Code: $config = array( //This line is existing Can you advise what is needed to define the function? As a matter of interest will that allow backspace?
Can somebody correct what Im doing wrong here?
PHP Code: $config = array(
Try this if you didn't solved yours issue yet
Code: $config = array(
(02-25-2021, 08:37 AM)brianjamestorr Wrote: Try this if you didn't solved yours issue yet I get this error message. Unable to access an error message corresponding to your field name Length.(regex_match/) Update Ive been thinking my problem maybe the version Im operating - const CI_VERSION = '3.1.9'; I cant find anything in the manual about regex_match so guess its in v4 |
Welcome Guest, Not a member yet? Register Sign In |