Welcome Guest, Not a member yet? Register   Sign In
How to disable/enable keys
#1

(This post was last modified: 02-07-2021, 09:15 AM by christaliise.)

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>
function 
accept_numeric_only(e) {
|| window.event;
var 
e.keyCode || e.which;
var 
upperlowercase e.shiftKey;
if(
uppercase && lowercase && <= && >= && <= 47 && >= 58 && <= 189 && >= 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 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?
Reply
#2

Test onkey() event you can controller char is typing
Enlightenment  Is  Freedom
Reply
#3

(02-07-2021, 12:14 PM)paliz Wrote: Test onkey() event you can controller char is  typing

Thanks @paliz, but I need more explanation.
Reply
#4

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-.@#$%&] 
Reply
#5

(02-10-2021, 01:33 AM)brianjamestorr Wrote: 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-.@#$%&] 

Thanks @brianjamestorr but is that Javascript, PHP or other?

Can you give an example of some coding?
Reply
#6

(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.

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-.@#$%&] 

Thanks @brianjamestorr but is that Javascript, PHP or other?

Can you give an example of some coding?


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.


Reply
#7

(This post was last modified: 02-14-2021, 10:38 AM by christaliise.)

(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.

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-.@#$%&] 

Thanks @brianjamestorr but is that Javascript, PHP or other?

Can you give an example of some coding?


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.



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
array('field' => 'length''label' => 'Length''rules' => 'required''errors' => array( 'required' => 'Provide%s')), //This line is existing
array('field' => 'length' ('''''regex_match[/0-9./]'); //This line obviously needs to be defined
$this->form_validation->set_rules($config); //This line is existing 

Can you advise what is needed to define the function?

As a matter of interest will that allow backspace?
Reply
#8

(This post was last modified: 02-18-2021, 08:25 AM by christaliise.)

Can somebody correct what Im doing wrong here?

PHP Code:
$config = array(
array(
'field' => 'length''label' => 'Length''rules' => 'required|regex_match/[0-9.]/'', 'errors' => array( 'required' => 'Provide%s', '%sNumbersOnly')));
$this->form_validation->set_rules($config); 
Reply
#9

Try this if you didn't solved yours issue yet
Code:
$config = array(
    array(
        'field' => 'length', // this must match yours <input name=""> field in form
        'label' => 'Length',
        'rules' => 'required|regex_match/[0-9.]/', // you had extra ' on end
        'errors' => array( 'required' => 'Provide%s', '%sNumbersOnly' )
    )
); // you didn't closed array here
Reply
#10

(This post was last modified: 02-26-2021, 02:06 PM by christaliise.)

(02-25-2021, 08:37 AM)brianjamestorr Wrote: Try this if you didn't solved yours issue yet
Code:
$config = array(
array(
'field' => 'length', // this must match yours <input name=""> field in form
'label' => 'Length',
'rules' => 'required|regex_match/[0-9.]/', // you had extra ' on end
'errors' => array( 'required' => 'Provide%s', '%sNumbersOnly' )
)
); // you didn't closed array here

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
Reply




Theme © iAndrew 2016 - Forum software by © MyBB