SOLVED

Prevent numbers in a text field

Go to solution
Anna_Blanchet1
Level 4

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!

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Prevent numbers in a text field

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

 

 

 

View solution in original post

2 REPLIES 2
SanfordWhiteman
Level 10 - Community Moderator

Re: Prevent numbers in a text field

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 4

Re: Prevent numbers in a text field

Thank you, Sanford! That solution worked perfectly!