[SalesForce] prevent web-to-lead form submit if condition are not met

I've added a new custom field on web-to-lead form (consent to contact-picklist),I want a user to be able to submit only if consent to contact=yes, by default it will appear as 'NO' on the form. Any help would be really appreciated.

Code:

<li>
  Lead Consent to Contact: 
  <select id="00Nw0000003cnpk" name="00Nw0000003cnpk" title="Consent to Contact"> 
    <option value="No">No</option>
    <option value="Yes">Yes</option>
  </select>
</li><br/>
<input type="submit" name="submit"/><br/></ul> 
</div></form>

Best Answer

You can use client side scripting to validate the inputs. You can either use JavaScript or jQuery for this.

You can find the free jQuery Validation Plugin which can be used for validation at this link.

But in your case, it's a simple validation, you can achieve it using simple Javascript as well. You can refer this link to get started on this.

One more way is to create a validation rule on Lead in Salesforce, this will prevent invalid leads from being created, but Salesforce won't return anything in response, hence user won't know the outcome.


Update

Your javascript code that you provided in comments has error, hence it won't work.You will need to do 3 things in your web to lead form

Change the Id of the Lead Consent field in the generated html to something readable, but make sure that you are not changing the value of name attribute.

Replace this below line in web to lead html

<select id="00Nw0000003cnpk" name="00Nw0000003cnpk" title="Consent to Contact">

with

<select id="leadconsent" name="00Nw0000003cnpk" title="Consent to Contact">

Add this below script in the form tag

<script type='text/javascript'> 
    function ValidateForm() { 
        if (document.getElementById('leadconsent').value == 'No') { 
            alert("Please change value YES for consent to contact"); 
            return false; 
        } 
        return true; 
    } 
</script>

And lastly, add this validation method to form tag i.e.

replace the below code

<form action="https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST">

with

<form action="https://webto.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8" method="POST" onsubmit="return ValidateForm()">
Related Topic