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
Dave The RaveDave The Rave 

HTML web-to-case form with field validation

Hi all,

I am struggling with validating input on a form. I am cutting and pasting the code below into a CMS system:

The form is submitting to SF even if you do not fill any fields in.

Does anyone have a solution:
 
<html>
<head>
<script>
function validateForm() {
  var x = document.forms["myForm"]["fname"].value;
  if (x == "") {
    alert("Veld verplicht*");
    return false;
  }
}
</script>
</head>
<body>

<form name="myForm" action="https://ako--abc.my.salesforce.com/servlet/servlet.WebToCase?encoding=UTF-8"  onsubmit="return validateForm()" method="post">

<input type="hidden" name="orgid" value="00D0Q0000008i66" /> 
      <input type="hidden" name="retURL" value="https://www.vierhetseizoen.nl/contact/bedankt" /> 
      <input type="hidden" name="status" value="New" /> 
      <input type="hidden" name="requesttype__c" value="Inbound" /> 
      <input type="hidden" name="type" value="Question" /> 
      <input type="hidden" name="recordtype" value="Order" />
      <input type="hidden" name="webcountry__c" value="NL" />

      <!-- Fields above not viewable in form but hidden data transmitted to SF case record  -->
      
      <div style="width: 90%; margin: auto;">
            <p style="padding:0px; margin-left: 5px;">name</p>
            <input id="b1518536294940" name="Voornaam" type="text" placeholder="Voornaam *" value="" data-type="text" required=true/>
 

          </div>

   <!-- Code for required fields test -->
   <br /> 

  <input type="submit" value="Submit">
</form>

</body>
</html>



Thanks Dave
ShirishaShirisha (Salesforce Developers) 
Hi Dave,

Greetings!

Can you please try by using required only in the input definition instead of using "required=true" in the below line:

<input id="b1518536294940" name="Voornaam" type="text" placeholder="Voornaam *" value="" data-type="text" required=true/>

However,I have tried with the above line and it is throwing an error when clicking on the Submit with the blank input.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Warm Regards,
Shirisha Pathuri