icon-arrow icon-check icon-mail icon-phone icon-facebook icon-linkedin icon-youtube icon-twitter icon-cheveron icon-download icon-instagram play close close icon-arrow-uturn icon-calendar icon-clock icon-search icon-chevron-process icon-skills icon-knowledge icon-kite icon-education icon-languages icon-tools icon-experience icon-coffee-cup
Werken bij Integration & Application Talents
Blog 11/02/2023

How do you enforce the use of only numbers and letters in an Apex field?

OnlyNumbersAndLetters

Michel Vogelpoel
Michel Vogelpoel /
Integratie expert

I had the task to ensure that a user was only allowed to use numbers and letters as a file name in 1 particular text field of Apex. 

During one of my previous assignments, I had already come up with a solution for being allowed to use only numbers in an Apex field (see here). But excluding the shift key and control key in combination of letters and numbers proved complicated.

As you do want customers to use a capital letter, but prevent customers adding an exclamation mark ( SHIFT+1) or doing a CTRL-V, I came up with the following properties on the Dynamic Action on the field:

The dynamic action

The Dynamic Action properties explained

Dynamic Action: Key Down (the action is triggered by a key being pressed)
Client Side condition, javascript expression: (the restriction of keys that may or may not be used)

( !event.ctrlKey && ( event.keyCode >= 65 && event.keyCode <= 90)) ||
( !event.shiftKey && ( event.keyCode >= 48 && event.keyCode <= 57) ) || 
( event.keyCode == 8 ) || 
( event.keyCode == 9 )


Translation of the above code:
  No control key may be held while pressing letters A to Z.
  No shift key may be held while pressing numbers 1 to 0.
  Backspace key is allowed.
  Tab key is allowed.


The expression can easely be modified if needed, to include for instance the @ you could add:

|| ( event.shiftKey && event.keyCode == 50 )

This site is excellent to find the keyCodes of your specific key pressing.


Go here for a working example in Apex.

Geen reacties

Geef jouw mening

Reactie plaatsen

Reactie toevoegen

Jouw e-mailadres wordt niet openbaar gemaakt.

Geen HTML

  • Geen HTML toegestaan.
  • Regels en alinea's worden automatisch gesplitst.
  • Web- en e-mailadressen worden automatisch naar links omgezet.
Michel Vogelpoel
Michel Vogelpoel /
Integratie expert

Wil je deel uitmaken van een groep gedreven en ambitieuze experts? Stuur ons jouw cv!