Search code examples
javascriptcontact-formverification

Need Javascript for Specific Response in Contact Form (text must equal "yes")


I currently have a contact form that requires a user to type 'yes' to an agreement. I have knowledge of HTML and CSS, but do not know the possible Javascript code to require the user to enter 'yes' in the text box and to prevent the message from going through if they were to enter 'no' or any other response other than 'yes'. Any assistance would be greatly appreciated. Thank you!


Solution

  • You can use pattern attribute and the form will take care of the validation

    <form>
      <label for="confirm">Type yes</label>
      <input type="text" id="confirm" name="confirm" placeholder="yes" pattern="^yes$" required>
      <input type="submit" />
    </form>