How to add validators for letters check? | Adobe Higher Education
Skip to main content
Level 2
December 23, 2024
Risolto

How to add validators for letters check?

  • December 23, 2024
  • 2 risposte
  • 523 visualizzazioni

How to add validators in a multifield with two textfields such that it only accepts letters in the fields? Possibly using js

Migliore risposta di konstantyn_diachenko

Hi @vodjakxa ,

 

I'd suggest using Field Validation in Granite UI: https://developer.adobe.com/experience-manager/reference-materials/6-5/granite-ui/api/jcr_root/libs/granite/ui/components/coral/foundation/clientlibs/foundation/js/validation/index.html

 

Example of JS client library (categories: cq.authoring.dialog.all):

(function ($, window) { const registry = $(window).adaptTo("foundation-registry"); registry.register("foundation.validation.validator", { selector: "[data-validation=letter-match]", validate: function (el) { const element = $(el); const letter = element.data('letter'); const value = element.val(); if (value.length !== 0 && letter && !value.includes(letter)) { return "The field must contain letter: " + letter; } } }); }) (jQuery, window);

 

Example of usage in dialog:

<myField jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/textfield" fieldDescription="Some description" fieldLabel="Some label" name="./myField" validation="letter-match"> <granite:data jcr:primaryType="nt:unstructured" letter="A"/> </phone>

 

Best regards,

Kostiantyn Diachenko.

2 risposte

arunpatidar
Community Advisor
Community Advisor
December 23, 2024
konstantyn_diachenko
Community Advisor
Community Advisor
December 23, 2024

Hi @vodjakxa ,

 

I'd suggest using Field Validation in Granite UI: https://developer.adobe.com/experience-manager/reference-materials/6-5/granite-ui/api/jcr_root/libs/granite/ui/components/coral/foundation/clientlibs/foundation/js/validation/index.html

 

Example of JS client library (categories: cq.authoring.dialog.all):

(function ($, window) { const registry = $(window).adaptTo("foundation-registry"); registry.register("foundation.validation.validator", { selector: "[data-validation=letter-match]", validate: function (el) { const element = $(el); const letter = element.data('letter'); const value = element.val(); if (value.length !== 0 && letter && !value.includes(letter)) { return "The field must contain letter: " + letter; } } }); }) (jQuery, window);

 

Example of usage in dialog:

<myField jcr:primaryType="nt:unstructured" sling:resourceType="granite/ui/components/coral/foundation/form/textfield" fieldDescription="Some description" fieldLabel="Some label" name="./myField" validation="letter-match"> <granite:data jcr:primaryType="nt:unstructured" letter="A"/> </phone>

 

Best regards,

Kostiantyn Diachenko.

Kostiantyn Diachenko, Community Advisor, Certified Senior AEM Developer, creator of free AEM VLT Tool, maintainer of AEM Tools plugin.