<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Custom validation &amp;amp; prevent form submision in APIs &amp; Integrations</title>
    <link>https://community.hubspot.com/t5/APIs-Integrations/Custom-validation-amp-prevent-form-submision/m-p/973029#M73458</link>
    <description>&lt;P&gt;Thank you so much for sharing!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've seen&amp;nbsp; your post so it's included under "Top Community Conversations"&lt;A href="https://arkansascourtconnect.us/" target="_blank" rel="noopener"&gt;on&lt;/A&gt;&lt;A href="https://community.hubspot.com/" target="_blank" rel="noopener"&gt;&amp;nbsp;homepage&lt;/A&gt;&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;</description>
    <pubDate>Wed, 08 May 2024 04:27:32 GMT</pubDate>
    <dc:creator>DavidClark08</dc:creator>
    <dc:date>2024-05-08T04:27:32Z</dc:date>
    <item>
      <title>Custom validation &amp; prevent form submision</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Custom-validation-amp-prevent-form-submision/m-p/882793#M69216</link>
      <description>&lt;P&gt;Hello all,&lt;BR /&gt;&lt;BR /&gt;Just found a solution for doing a basic validation on fields before submit on a hubspot form.&lt;BR /&gt;&lt;BR /&gt;Conditions:&lt;BR /&gt;Raw html form&lt;BR /&gt;Include jquery&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;Code&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;&amp;lt;script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/embed/v2.js"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script&amp;gt;
  hbspt.forms.create({
    region: "...",
    portalId: "123123",
    formId: "xxxxxxx", 
    onFormReady: function () {
      // Use a more specific selector if possible
      var formSelector = '#hsForm_xxxxxx'; // Replace with your form's specific class or ID
      var form = document.querySelector(formSelector);

      if (form) {
        var firstnameField = form.querySelector('[name="firstname"]');
        var submitButton = form.querySelector('input[type="submit"], button[type="submit"]');

        if (firstnameField &amp;amp;&amp;amp; submitButton) {
          // Initially disable the submit button
          submitButton.disabled = true;

          firstnameField.addEventListener('input', function() {
            var value = firstnameField.value;
            if (isValid(value)) {
                clearError();
                submitButton.disabled = false;
            } else {
                showError('The field format is invalid. It should be 2 letters followed by 8 numbers.');
                submitButton.disabled = true;
            }
          });
        } else {
          console.error('Form elements not found');
        }
      } else {
        console.error('Form not found');
      }

      function isValid(value) {
        var regex = /^[A-Za-z]{2}\d{8}$/; // This matches XX12345678
        return regex.test(value);
      }

      function showError(message) {
        console.error(message); // Placeholder implementation
        // Add UI logic here to display the message
      }

      function clearError() {
        // Add UI logic here to clear the displayed error message
      }
    }
  });
&amp;lt;/script&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;This code will disable the submit button until the field matches the regex.&lt;BR /&gt;You can add as many form validations you like and also add error message until the fields matches the regex.&lt;BR /&gt;&lt;BR /&gt;Hope this helps.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Nov 2023 16:23:05 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Custom-validation-amp-prevent-form-submision/m-p/882793#M69216</guid>
      <dc:creator>CVlad</dc:creator>
      <dc:date>2023-11-21T16:23:05Z</dc:date>
    </item>
    <item>
      <title>Re: Custom validation &amp; prevent form submision</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Custom-validation-amp-prevent-form-submision/m-p/883196#M69249</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.hubspot.com/t5/user/viewprofilepage/user-id/346360"&gt;@CVlad&lt;/a&gt;,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you so much for sharing!&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":yellow_heart:"&gt;💛&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I've highlighted your post so it's included under "Top Community Conversations"&lt;A href="https://community.hubspot.com/" target="_blank" rel="noopener"&gt; on our homepage&lt;/A&gt;. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheers&lt;BR /&gt;Mia, Community Team&lt;/P&gt;</description>
      <pubDate>Wed, 22 Nov 2023 11:28:09 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Custom-validation-amp-prevent-form-submision/m-p/883196#M69249</guid>
      <dc:creator>MiaSrebrnjak</dc:creator>
      <dc:date>2023-11-22T11:28:09Z</dc:date>
    </item>
    <item>
      <title>Re: Custom validation &amp; prevent form submision</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Custom-validation-amp-prevent-form-submision/m-p/884148#M69286</link>
      <description>&lt;P&gt;Thanks this looks great&lt;BR /&gt;&lt;BR /&gt;Previously I've set the validation on the click event on the button in the onFormReady&lt;BR /&gt;&lt;BR /&gt;Although it's a 'click' event apparently the click is triggered when someone hits enter too&lt;BR /&gt;&lt;BR /&gt;e.g. in quick jQuery&lt;/P&gt;
&lt;LI-CODE lang="javascript"&gt;$(submitButton).on('click',function(e){
  if(do some custom validation) {
    e.preventDefault();
  }
}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;That code needs to be in the onFormReady and you obviously need to use the correct selectors&lt;/P&gt;</description>
      <pubDate>Fri, 24 Nov 2023 10:23:19 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Custom-validation-amp-prevent-form-submision/m-p/884148#M69286</guid>
      <dc:creator>matt_scott</dc:creator>
      <dc:date>2023-11-24T10:23:19Z</dc:date>
    </item>
    <item>
      <title>Re: Custom validation &amp; prevent form submision</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Custom-validation-amp-prevent-form-submision/m-p/888679#M69489</link>
      <description>&lt;P&gt;There are some great ideas on form functionality extensions here and elsewhere in the community. I've found this incredibly helpful, but its sometime hard to hunt for.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there any chance that someone could formally update the foundationn document for form extensions at&amp;nbsp;&lt;A href="https://legacydocs.hubspot.com/docs/methods/forms/advanced_form_options" target="_blank"&gt;https://legacydocs.hubspot.com/docs/methods/forms/advanced_form_options&lt;/A&gt;&amp;nbsp;with some of the more complete and innovative functionality thats been discussed here?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Steve&lt;/P&gt;</description>
      <pubDate>Mon, 04 Dec 2023 15:01:44 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Custom-validation-amp-prevent-form-submision/m-p/888679#M69489</guid>
      <dc:creator>SteveHTM</dc:creator>
      <dc:date>2023-12-04T15:01:44Z</dc:date>
    </item>
    <item>
      <title>Re: Custom validation &amp; prevent form submision</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Custom-validation-amp-prevent-form-submision/m-p/973029#M73458</link>
      <description>&lt;P&gt;Thank you so much for sharing!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I've seen&amp;nbsp; your post so it's included under "Top Community Conversations"&lt;A href="https://arkansascourtconnect.us/" target="_blank" rel="noopener"&gt;on&lt;/A&gt;&lt;A href="https://community.hubspot.com/" target="_blank" rel="noopener"&gt;&amp;nbsp;homepage&lt;/A&gt;&lt;/P&gt;&lt;P&gt;.&lt;/P&gt;</description>
      <pubDate>Wed, 08 May 2024 04:27:32 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Custom-validation-amp-prevent-form-submision/m-p/973029#M73458</guid>
      <dc:creator>DavidClark08</dc:creator>
      <dc:date>2024-05-08T04:27:32Z</dc:date>
    </item>
    <item>
      <title>Re: Custom validation &amp; prevent form submision</title>
      <link>https://community.hubspot.com/t5/APIs-Integrations/Custom-validation-amp-prevent-form-submision/m-p/1098998#M79800</link>
      <description>&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;P&gt;Custom validation is a great way to ensure data integrity before a form submission. I have implemented custom validation in JavaScript to check for specific conditions like input length or format, and prevent submission if they are not met. It is always important to give clear error messages to guide the user. Anyone else have tips for handling more complex validation scenarios?&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Fri, 24 Jan 2025 08:46:19 GMT</pubDate>
      <guid>https://community.hubspot.com/t5/APIs-Integrations/Custom-validation-amp-prevent-form-submision/m-p/1098998#M79800</guid>
      <dc:creator>kgeddon</dc:creator>
      <dc:date>2025-01-24T08:46:19Z</dc:date>
    </item>
  </channel>
</rss>

