APIs & Integrations

PaulsPerkons
Teilnehmer/-in

Limit Character Count for Form Field

lösung

Hi,

I have a form for which I have to make one of the fields allow a limited amount of characters to be input (minimum 2, maximum 12).

I found a solution mentioned in a different topic, which I tried to use, however, when I insert the actual code (the onformready), the form does not load anymore. Any possible way how I could approach this?

hbspt.forms.create({
portalId: "XXX",
formId: "XXXXXXX",
css: "",
onFormReady: function($form) {
$(“input[name="vat_number"]”).attr(‘maxlength’,‘12’);
$(“input[name="vat_number"]”).attr(‘minlength’,‘2’);
} ,
});

1 Akzeptierte Lösung
Derek_Gervais
Lösung
HubSpot-Alumnus/Alumna
HubSpot-Alumnus/Alumna

Limit Character Count for Form Field

lösung

Hi @paulsperkons,

You'll want to chain .change() to the end of each call, and also replace the left and right double quotes with standard double quotes (i.e. " )

Lösung in ursprünglichem Beitrag anzeigen

5 Antworten
WTariq
Mitglied

Limit Character Count for Form Field

lösung

Hi All,

I figured it out this way:

hbspt.forms.create({
        portalId: "123456",
        formId: "abcd-efg-hijk=lmno",
        onFormReady: function($form) {
          $form.find('textarea[name="message"]').attr('maxlength','150');
          $form.find('input[name="what_are_you_planning"]').attr('maxlength','150');
        },
  });
0 Upvotes
Derek_Gervais
HubSpot-Alumnus/Alumna
HubSpot-Alumnus/Alumna

Limit Character Count for Form Field

lösung

Hi @Trevor_Hatfield,

name is the input's name attribute, which for HubSpot forms is the internal property name of the corresponding contact property. I'm not entirely sure why your code isn't working; can you give me a link to the page you're working on?

Derek_Gervais
Lösung
HubSpot-Alumnus/Alumna
HubSpot-Alumnus/Alumna

Limit Character Count for Form Field

lösung

Hi @paulsperkons,

You'll want to chain .change() to the end of each call, and also replace the left and right double quotes with standard double quotes (i.e. " )

Trevor_Hatfield
Mitglied

Limit Character Count for Form Field

lösung

Trying to set a character limit on a text field to 250 characters, but its not working. The form just submits no matter what without checking the character limitation. Any help with getting this working would be awesome.

Is the name= the id of the form field or something else?

hbspt.forms.create({
portalId: "XXXXX",
formId: "XXXXXXXXXXXX",
css: "",
onFormReady: function($form) {
$('input[name="closed_lost_notes"]').attr('minlength','250').change();
$('input[name="closed_lost_notes"]').attr('maxlength','2500').change();
},
});

Also is there a way to show the character count and custom validation message if submitted before reaching the 250 character limit?

Thanks so much for any help!

PaulsPerkons
Teilnehmer/-in

Limit Character Count for Form Field

lösung

Thank you. This fixed it.