Skip to content

JavaScript contact form validation in HTML | Example code

What if the visitor filled the wrong info in the contact form, to prevent wrong entry from visitors on the website, that we use JavaScript to validate contact form fields.

A contact form is basically a set of fields (Name, email id, contact number, and question) on the webpage, Where visitors will fill it and click on the send button. That is automatically sent to the website owner’s email id.

Must Read:-

Example of JavaScript contact form validation

In the example we using 4 input fields:

  • Name
  • Phone number
  • Email Address
  • Your messages

It’s validate dynamically, if found any wrong data will highlight with message.

<!DOCTYPE html>
 <html>
 <head>
  <title>Contact us Form</title>

  <script type="text/javascript">
    function validateName() {

      var name = document.getElementById('contact-name').value;

      if(name.length == 0) {

        producePrompt('Name is required', 'name-error' , 'red')
        return false;

    }

    if (!name.match(/^[A-Za-z]*\s{1}[A-Za-z]*$/)) {

        producePrompt('First and last name, please.','name-error', 'red');
        return false;

    }

    producePrompt('Valid', 'name-error', 'green');
    return true;

}

function validatePhone() {

  var phone = document.getElementById('contact-phone').value;

  if(phone.length == 0) {
      producePrompt('Phone number is required.', 'phone-error', 'red');
      return false;
  }

  if(phone.length != 10) {
      producePrompt('Include area code.', 'phone-error', 'red');
      return false;
  }

  if(!phone.match(/^[0-9]{10}$/)) {
      producePrompt('Only digits, please.' ,'phone-error', 'red');
      return false;
  }

  producePrompt('Valid', 'phone-error', 'green');
  return true;

}

function validateEmail () {

  var email = document.getElementById('contact-email').value;

  if(email.length == 0) {

    producePrompt('Email Invalid','email-error', 'red');
    return false;

}

if(!email.match(/^[A-Za-z\._\-[0-9]*[@][A-Za-z]*[\.][a-z]{2,4}$/)) {

    producePrompt('Email Invalid', 'email-error', 'red');
    return false;

}

producePrompt('Valid', 'email-error', 'green');
return true;

}

function validateMessage() {
  var message = document.getElementById('contact-message').value;
  var required = 30;
  var left = required - message.length;

  if (left > 0) {
    producePrompt(left + ' more characters required','message-error','red');
    return false;
}

producePrompt('Valid', 'message-error', 'green');
return true;

}

function validateForm() {
  if (!validateName() || !validatePhone() || !validateEmail() || !validateMessage()) {
    jsShow('submit-error');
    producePrompt('Please fix errors to submit.', 'submit-error', 'red');
    setTimeout(function(){jsHide('submit-error');}, 2000);
    return false;
}
else {

}
}

function jsShow(id) {
  document.getElementById(id).style.display = 'block';
}

function jsHide(id) {
  document.getElementById(id).style.display = 'none';
}


function producePrompt(message, promptLocation, color) {

  document.getElementById(promptLocation).innerHTML = message;
  document.getElementById(promptLocation).style.color = color;

}

</script>
</head>

<body>
    <form>

      <div class="form-group">
        <label for="contact-name">Name</label>
        <input type="text" class="form-control" id="contact-name" name="name"       placeholder="Enter your name.." onkeyup='validateName()'>
        <span class='error-message' id='name-error'></span>
    </div>

    <div class="form-group">
        <label for="contact-phone">Phone Number</label>
        <input type="tel" class="form-control" id="contact-phone" name="phone" placeholder="Ex: 1231231234" onkeyup='validatePhone()'>
        <span class='error-message' id='phone-error'></span>
    </div>

    <div class="form-group">
        <label for="contact-email">Email address</label>
        <input type="email" class="form-control" id="contact-email" name="email" placeholder="Enter Email" onkeyup='validateEmail()'>
        <span class='error-message' id='email-error'></span>
    </div>   

    <div class="form-group">
        <label for='contactMessage'>Your Message</label>
        <textarea class="form-control" rows="5" id='contact-message'  name='message'  placeholder="Enter a brief message" onkeyup='validateMessage()'></textarea>
        <span class='error-message' id='message-error'></span>
    </div>

    <button onclick='return validateForm()' class="btn btn-default">Submit</button>
    <span class='error-message' id='submit-error'></span>
</form>
</body>
</html>

Output:

JavaScript contact form validation HTML

Do comment if you have any code query or suggestion on this topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

1 thought on “JavaScript contact form validation in HTML | Example code”

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading