Prevent numbers in a text field | Community
Skip to main content
Anna_Blanchet1
New Participant
October 23, 2023
Solved

Prevent numbers in a text field

  • October 23, 2023
  • 1 reply
  • 1528 views

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.
Best answer by 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 }); }); });

 

 

 

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
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
New Participant
October 24, 2023

Thank you, Sanford! That solution worked perfectly!