Skip to main content
Anna_Blanchet1
Level 3
Oct 23, 2023 9:19 PM UTC
Solved

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!

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
SanfordWhiteman
SanfordWhitemanAccepted solution
User Group Leader
Oct 24, 2023 12:42 AM UTC

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 replies

SanfordWhiteman
User Group Leader
SanfordWhitemanUser Group LeaderAccepted solution
User Group Leader
Oct 24, 2023 12:42 AM UTC

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 }); }); });

 

 

 

Translate
Anna_Blanchet1
Level 3
Oct 24, 2023 2:27 PM UTC

Thank you, Sanford! That solution worked perfectly!

Translate