Last active
January 18, 2016 20:58
-
-
Save isaumya/b68fcd793a8eecc25d78 to your computer and use it in GitHub Desktop.
Validation for WordPress Default Comments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery(function ($) { | |
/*Email validation function*/ | |
function isEmail(email) { | |
var regex = /^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/; | |
if( regex.test(email) ){ | |
return true; | |
} else { | |
return false; | |
} | |
} | |
/*Blog Post comment validation - keeping post comment button disabled until everything satisfy*/ | |
if ( ! $('body.single-post .logged-in-as').length ) { | |
/*Set Comment Submit Button disabled*/ | |
$('body.single-post #submit').attr('disabled', 'disabled'); | |
/*appending the required span fields to show notice*/ | |
if ( $('#commentform #author').val() == '' ) { | |
$('.comment-form-author').append('<span id="nameNotice">Please enter your name</span>'); | |
} | |
if ( $('#commentform #email').val() == '' ) { | |
$('.comment-form-email').append('<span id="emailNotice">Please enter a valid email</span>'); | |
} | |
$('.comment-form-comment').append('<span id="commentNotice">Comment must be at least 15 charecters long</span>'); | |
$('.form-submit').append('<p id="submitNotice">Please fill up the above form properly to enable the post comment button.</p>'); | |
$('#commentform').keyup(function() { | |
/*Checking if name is provided*/ | |
if ( $('#commentform #author').val().replace(/ /g,'') == '' ) { | |
$('#nameNotice').text( 'Please enter your name' ); | |
} else { | |
$('#nameNotice').text( '' ); | |
} | |
/*Checking if email is provided*/ | |
if ( $('#commentform #email').val().replace(/ /g,'') == '' ) { | |
$('#emailNotice').text( 'Please enter a valid email' ); | |
} else if ( isEmail( $('#commentform #email').val().replace(/ /g,'') ) == false ) { | |
$('#emailNotice').text( 'This is not a valid email' ); | |
} else { | |
$('#emailNotice').text( '' ); | |
} | |
/*Checking if comment is provided*/ | |
if ( $('#commentform #comment').val().replace(/ /g,'') == '' ) { | |
$('#commentNotice').text( 'Comment must be at least 15 charecters long' ); | |
} else if ( $('#commentform #comment').val().replace(/ /g,'').length <= 14 ) { | |
$('#commentNotice').text( ( 15 - ( $('#commentform #comment').val().replace(/ /g,'').length ) ) + ' more charecters to go ...' ); | |
} else { | |
$('#commentNotice').text( '' ); | |
} | |
if ( ( $('#commentform #author').val().replace(/ /g,'') != '' ) && | |
( $('#commentform #email').val().replace(/ /g,'') != '' ) && | |
( isEmail( $('#commentform #email').val().replace(/ /g,'') ) == true ) && | |
( $('#commentform #comment').val().replace(/ /g,'').length > 14 ) | |
){ | |
$('body.single-post #submit').removeAttr('disabled'); | |
$('#submitNotice').text(''); | |
} else { | |
$('body.single-post #submit').attr('disabled', 'disabled'); | |
$('#submitNotice').text('Please fill up the above form properly to enable the post comment button.'); | |
} | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment