Skip to main content
Anna_Blanchet1
Level 3
October 23, 2023
Répondu

Prevent numbers in a text field

Is there a way to prevent numbers from being submitted in a text field on a Marketo form? I tried the masking feature but we need the number of characters to be flexible.  Thanks in advance for your input!

Ce sujet a été fermé aux réponses.
Meilleure réponse par SanfordWhiteman

Yes. The method is somewhat tricky and is not specifically mentioned in my More input mask tweaks post — which you should read anyway!

 

First make sure the input has a mask in Form Editor (it doesn’t matter what the mask is, but you need it to enable masks in general).

 

Then add a new custom mask character. We’ll use D, which will allow non-digit or empty at any position.

 

Then you can set the mask to 255 x D for a field with special placeholder and autoclear options. This is equivalent to allowing up to 255 non-digits:

 

 

MktoForms2.$("body").on("mkto_inputmask_polyfilled", function(e) { MktoForms2.whenReady(function(readyForm) { // new mask "D" = non-digit or empty const maskCharExtensions = { "D": /^(\D|)$/ }; Object.keys(maskCharExtensions) .forEach(function(char) { MktoForms2.$.mask.definitions[char] = maskCharExtensions[char]; }); }); MktoForms2.whenReady(function(readyForm){ MktoForms2.$("[name='LastName']").mask("D".repeat(255), { placeholder: "", autoclear: false }); }); });

 

 

 

2 commentaires

SanfordWhiteman
Level 10
October 24, 2023

Yes. The method is somewhat tricky and is not specifically mentioned in my More input mask tweaks post — which you should read anyway!

 

First make sure the input has a mask in Form Editor (it doesn’t matter what the mask is, but you need it to enable masks in general).

 

Then add a new custom mask character. We’ll use D, which will allow non-digit or empty at any position.

 

Then you can set the mask to 255 x D for a field with special placeholder and autoclear options. This is equivalent to allowing up to 255 non-digits:

 

 

MktoForms2.$("body").on("mkto_inputmask_polyfilled", function(e) { MktoForms2.whenReady(function(readyForm) { // new mask "D" = non-digit or empty const maskCharExtensions = { "D": /^(\D|)$/ }; Object.keys(maskCharExtensions) .forEach(function(char) { MktoForms2.$.mask.definitions[char] = maskCharExtensions[char]; }); }); MktoForms2.whenReady(function(readyForm){ MktoForms2.$("[name='LastName']").mask("D".repeat(255), { placeholder: "", autoclear: false }); }); });

 

 

 

Anna_Blanchet1
Level 3
October 24, 2023

Thank you, Sanford! That solution worked perfectly!