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.
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.
<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>
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
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)
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