Javascript insert to prevent form submission based on field formatting

Hi all, I am trying to prevent form submission based on the phone formatting. To do that I have a javascript which I need to insert into the form. I have attempted the insertion within a form element, i have tried to insert it as part of a custom reg page, and finally as part of a custom landing page. None of this has resulted in the form preventing submission if the phone number was wrong. I am unsure of the method here, but if you can help me out it would be much appreciated. Please feel free to suggest a Javascript template if indeed it is different from a standard javascript.

Thanks a lot,

MIckael

Can you provide the code you’re using?

I’d make sure you’re using the right selectors for selecting the phone field you’re validating, as well as the submit button you’re disabling, as well has whatever messaging you’re using to indicate the formatting is invalid.

Hey Michael, thanks so much here it is:

Be sure to use the Preformatted text option:
image

<script type="text/javascript">
  // Function to validate the phone number
  function validatePhoneNumber() {
    var phoneNumberField = jQuery(inf_field_Phone1).val(); // Replace 'inf_custom_PhoneNumber' with the actual field ID of the phone number field in your form
    // Define the allowed starting digits
    var allowedStartingDigits = ['234']; // Add your desired starting digits in this array
    // Check if the phone number starts with any of the allowed digits
    var startsWithAllowedDigit = allowedStartingDigits.some(function(digit) {
      return phoneNumberField.startsWith(digit);
    });
    // Check if the phone number exceeds 10 digits
    var exceedsMaxDigits = phoneNumberField.replace(/[^0-9]/g, '').length > 10;
    // If the phone number doesn't meet the criteria, prevent form submission
    if (!startsWithAllowedDigit || exceedsMaxDigits) {
      alert('Please enter a valid phone number.'); // Modify this alert message as per your requirement
      return false; // Prevent form submission
    }
    // If the phone number is valid, allow form submission
    return true;
  }
  // Attach the form submission event listener
  jQuery(document).ready(function() {
    jQuery(inf_form_74a7496a29a09933682b26c6fd933122).submit(function() {
      return validatePhoneNumber();
    });
  });
</script>

@Michael_Earl are you still with me on this? :slight_smile:

You might need to wrap the inf_field_Phone1 in single quotes, like this 'inf_field_Phone1', and similarly with 'inf_form_74a7496a29a09933682b26c6fd933122'.

Ok let’s try, @Michael_Earl do you suppose that it should inserted on a custom reg page, or in the form thank you page? Or on the form? Or on a custom thank you page? PLease let me know what technique you advise as I have tried them all so far. M

@Michael_Earl just following up here in terms of where to insert the javascript. Let me know if you have any tips. Thanks!

(Sorry typed out a response but I guess I didn’t hit reply!)

The provided code seems to suggest being placed alongside the form elements (specifically the phone number), so not alongside a thank you page.

I’ll try to test this out in my app in a bit and get back to you with my findings!

Okay thanks @Michael_Earl . For the record i was using a “landing page submitted” component in a campaign for this form submission process. Thanks, M

I was not able to get the code working in its current state. I was adding it as a snippet, and using the hosted form. If you host the form (by copying the HTML), then you might have better luck, but that will also require some editing of the provided HTML (you’ll notice there is a submit action which is likely conflicting with the code you’re trying to add: onsubmit="submitWebForm()" which you can modify if you’re hosting it yourself)

You can find some info in this comment:
Do something with Campaign Web Form before POST submission to infusionsoft - Jul '20

Hope this helps!

Just an update here. The above hasn’t worked at all. Please let me know if somehow someone can review the script for me, but this has been an issue still. Thanks, Mickael

@Michael_Earl can I touch base with you on this one? I don’t think my script is triggering at all. Any chance we can review this together?