Title: Comment form validation
Last modified: January 29, 2021

---

# Comment form validation

 *  [Subhro](https://wordpress.org/support/users/subhro2321/)
 * (@subhro2321)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/comment-form-validation-2/)
 * I was looking for a a fix which can show comment error in the comment form below
   fields. I have found the code below from a website. It works but there I want
   to add a rule which only allows emails with **[@gmail](https://wordpress.org/support/users/gmail/).
   com** and allow comments **minimum of 50 characters** and **maximum of 500 characters.**
 * Can somebody help in adding the email and comment rule in the code below plz?
   If the code is wrong then please guide me with a better code.
 * Thanks in advance!
 *     ```
       function comment_validation_init() {
           if(is_single() && comments_open() ) { ?>        
           <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
           <script type="text/javascript">
           jQuery(document).ready(function($) {
           $('#commentform').validate({
   
           rules: {
             author: {
               required: true,
               minlength: 2
             },
   
             email: {
               required: true,
               email: true
             },
   
             comment: {
               required: true,
               minlength: 20
             }
           },
   
           messages: {
             author: "Please fill the required field",
             email: "Please enter a valid email address.",
             comment: "Please fill the required field"
           },
   
           errorElement: "div",
           errorPlacement: function(error, element) {
             element.after(error);
           }
   
           });
           });
           </script>
           <?php
           }
           }
           add_action('wp_footer', 'comment_validation_init');
       ```
   
    -  This topic was modified 5 years, 8 months ago by [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/).

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/comment-form-validation-2/#post-13977256)
 * The rule syntax is unique to the validation plugin. I recommend asking in the
   jQuery Plugins Forum where expert users of the plugin can help you. Support links
   are at the bottom of the plugin’s home page:
    [https://jqueryvalidation.org/](https://jqueryvalidation.org/)
 * Users can disable scripts and submit comments outside of your criteria. If it’s
   critical the criteria be followed, you’ll also need validation server side with
   PHP code.
 *  Thread Starter [Subhro](https://wordpress.org/support/users/subhro2321/)
 * (@subhro2321)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/comment-form-validation-2/#post-13977288)
 * [@bcworkz](https://wordpress.org/support/users/bcworkz/), I am not sure about
   this. As I was surfing for finding a fix so that:
 * 1. comment errors are shown in the same form and below the comment input fields.
   
   2. The email field in comment will only allow emails with gmail.com
 * So, I wasn’t sure about the above code neither. In that case, Just let’s forget
   about the above code and I would request you to help me in this in your way.
 * I just don’t want to sue another plugin, if there’s some code to be added in 
   function.php, then it would be really great.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 8 months ago](https://wordpress.org/support/topic/comment-form-validation-2/#post-13980089)
 * Validating forms client side is a good practice for an optimal user experience.
   The problem is any client side validation can be circumvented. You still need
   to re-validate server side any fields where a certain range of values is required.
   I don’t have any code I can offer, but I can explain what I’d do.
 * Form fields under HTML5 have halfway decent validation built in to their attributes.
   I rely on those to the extent possible. You can even require a gmail address 
   be used through the `pattern` attribute which matches input to a regexp. Where
   added validation is needed, I’d rely upon straight JavaScript. IMO, loading jQuery
   and related plugins seems like overkill much of the time.
 * I generally call a field’s validation script from the field’s onchange attribute.
   Under the field’s HTML I include an empty div element where my JS can place any
   error messaging. Thus users get immediate feed back should there be a problem
   with their input. The built in field validation works similarly. Example field:
 *     ```
       <input type="text" id="country_code" name="country_code"
         pattern="[A-Za-z]{3}" title="Three letter country code"
         onchange="extraJsValidation();">
       <div id="country_err"></div>
       ```
   
 * Once the user completes the form with all client side validation being successful,
   they can submit the form. Server side, all validation is repeated, and every 
   field is sanitized. Only then is the data saved or otherwise processed.

Viewing 3 replies - 1 through 3 (of 3 total)

 The topic ‘Comment form validation’ is closed to new replies.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 2 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [5 years, 8 months ago](https://wordpress.org/support/topic/comment-form-validation-2/#post-13980089)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
