function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ashley FreiheitAshley Freiheit 

Validate web to lead form

Hello, 

I'm creating a web to lead form. One of the fields is supposed to be numeric values only, but I cannot create it as a number field because there cannot be commas in the values. I made it a text field and am attempting to create a validation that only allows numeric values, and doesn't allow the form to be submitted and throws an error to the person filling out the web form. I've added the below code to my form, but I'm not getting an error and leads are being created. Can someone spot check my javascript, as I'm totally new to this. Note: I do have ValidateForm as my form action upon submit. 

<script type='text/javascript'>
    function ValidateForm() {
        if  (isNaN(document.getElementById('MemberAccountNumber').value {
            alert("Member Account Number must only contain numeric values");
            return false;
        }
        return true;
    }
</script>
<script type='text/javascript'>
    function ValidateForm() {
        if  (isNaN(document.getElementById('MemberAccountNumber').value {
            alert("Member Account Number must only contain numeric values");
            return false;
        }
        return true;
    }
</script>



Thank you
 
Best Answer chosen by Ashley Freiheit
SubratSubrat (Salesforce Developers) 
Hello Ashley,

You are missing a closing parenthesis after document.getElementById('MemberAccountNumber').value.
<script type='text/javascript'>
    function ValidateForm() {
        if (isNaN(document.getElementById('MemberAccountNumber').value)) {
            alert("Member Account Number must only contain numeric values");
            return false;
        }
        return true;
    }
</script>

Make sure to include this JavaScript code within the <script> tags in your HTML web-to-lead form. Additionally, ensure that the form's submit button is calling the ValidateForm() function within the onsubmit attribute, like this:
<form action="your-action-url" method="POST" onsubmit="return ValidateForm()">
    <!-- Your form fields -->
    <input type="text" id="MemberAccountNumber" name="MemberAccountNumber" />
    <!-- Other fields and submit button -->
</form>

If this helps , please mark this as Best Answer.
Thank you.

All Answers

SubratSubrat (Salesforce Developers) 
Hello Ashley,

You are missing a closing parenthesis after document.getElementById('MemberAccountNumber').value.
<script type='text/javascript'>
    function ValidateForm() {
        if (isNaN(document.getElementById('MemberAccountNumber').value)) {
            alert("Member Account Number must only contain numeric values");
            return false;
        }
        return true;
    }
</script>

Make sure to include this JavaScript code within the <script> tags in your HTML web-to-lead form. Additionally, ensure that the form's submit button is calling the ValidateForm() function within the onsubmit attribute, like this:
<form action="your-action-url" method="POST" onsubmit="return ValidateForm()">
    <!-- Your form fields -->
    <input type="text" id="MemberAccountNumber" name="MemberAccountNumber" />
    <!-- Other fields and submit button -->
</form>

If this helps , please mark this as Best Answer.
Thank you.
This was selected as the best answer
Ashley FreiheitAshley Freiheit
@Subrat - that did it, thank you!