javascript - WordPress comment validation -
i created validation function validating email address , textrea content in wordpress comment:
<script> function validatetext(){ var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-za-z\-0-9]+\.)+[a-za-z]{2,}))$/; var email = document.getelementbyid('email').value; if (email == '' || !re.test($email)) { alert("please enter correct email address") return false; } else if(document.getelementbyid("comment").value.length == ""){ alert("please add comment") return false; } return true; } </script>
the above javascript code not work expected. there grammatical errors in it?
i added onclick="return validatetext();" submit button:
<input name="submit" type="submit" id="submit" class="submit" value="" onclick="return validatetext();" />
thanks.
add jquery , css in function.php
function pbd_vc_scripts() { if(is_single() ) { wp_enqueue_script( 'jquery-validate', get_template_directory_uri(). '/js/validator.min.js', array('jquery'), '1.10.0', true ); wp_enqueue_style( 'jquery-validate', get_template_directory_uri() . '/css/style.css', array(), '1.0' ); } } add_action('template_redirect', 'pbd_vc_scripts');
validation in comment. put on function.php
function pbd_vc_init() { ?> <script type="text/javascript"> jquery(document).ready(function($) { $('#commentform').validate({ rules: { author: { required: true, minlength: 2 }, email: { required: true, email: true }, /*url: { required: true },*/ /*phone: { required: true },*/ comment: { required: true, minlength: 20 } }, messages: { author: "please enter name.", email: "please enter email address.", /*url: "please enter website.",*/ /*phone:"please enter mobile number",*/ comment: "message must @ least 20 characters." } }); }); </script> <?php } add_action('wp_footer', 'pbd_vc_init', 999);
Comments
Post a Comment