WordPress comment form validation

wordpressCopy and paste below and see the validation in working:

<style type=”text/css”>
.error { font-size: 12px; padding: 0 0 5px 0; color: #FF0000; }
input.error, textarea.error { background: #FFD2D2; color:#000000; }
</style>
<?php
function comment_validation_init() {
if(is_single() && comments_open() ) { ?>
<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js”></script>
<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 enter your first name.”,
email: “Please enter a valid email address.”,
comment: “Comment box can’t be empty! It must be more than 20 characters.”
},

errorElement: “div”,
errorPlacement: function(error, element) {
element.before(error);
}
});
});
</script>
<?php
}
}
add_action(‘wp_footer’, ‘comment_validation_init’);
?>

Like to share it

1 Comment. Leave new

wordpress developer
June 7, 2017 7:07 am

Very good tutorial Thanks for the code…very nice.

Thanks again.

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *