javascript - Comment form validation

How can I set the validation rules for the comment field?I change the value of commenter namee-mailhomepage onmouseove

How can I set the validation rules for the comment field?

I change the value of commenter name/e-mail/homepage onmouseover and onblur (I use this instead of labels - so if the field is empty it displays "Your e-mail", "Your homepage", etc.). The problem is, on submit, it submits this text in the homepage field (since it has no validation unlike the e-mail field where you get an error if you entered anything except [email protected]).

How could I validate the homepage field?

How can I set the validation rules for the comment field?

I change the value of commenter name/e-mail/homepage onmouseover and onblur (I use this instead of labels - so if the field is empty it displays "Your e-mail", "Your homepage", etc.). The problem is, on submit, it submits this text in the homepage field (since it has no validation unlike the e-mail field where you get an error if you entered anything except [email protected]).

How could I validate the homepage field?

Share Improve this question asked Oct 4, 2010 at 11:50 Kilgore_TroutKilgore_Trout 411 gold badge1 silver badge2 bronze badges 3
  • There was similar question about form validation in admin area, answer there might fit your needs: Validating Custom Meta Box Values & Required Fields – Rarst Commented Oct 4, 2010 at 12:23
  • Thanks, but it seems that it can't change the entered value, so if somebody don't write an URL to the "homepage" field, it would simply return an error message (since "Your homepage" it is not a vaild URL). I need it submit the field as empty, so wordpress wouldn't create a nonsensical link "your°%20homepage"). – Kilgore_Trout Commented Oct 4, 2010 at 13:27
  • Ah, slightly misunderstood your question. This probably can be done in WordPress internally, but I don't know how - will need to look into it. As for front-end solution also check Sliding Labels, it's what I used at my blog for tidy inline labels with bit of eye-candy. – Rarst Commented Oct 4, 2010 at 14:53
Add a comment  | 

1 Answer 1

Reset to default 9

Comments processing is done in the file: wp-comments-post.php. You can use the hook pre_comment_on_post to validate the values entered in the comment form fields.

function custom_validate_comment_url() {
    if( !empty( $_POST['url'] ) && !preg_match( '\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[-A-Z0-9+&@#/%=~_|]', $_POST['url'] ) // do you url validation here (I am not a regex expert)
        wp_die( __('Error: please enter a valid url or leave the homepage field empty') );
}

add_action('pre_comment_on_post', 'custom_validate_comment_url');

if you want to change a submitted value, use the filter preprocess_comment. E.g.:

function custom_change_comment_url( $commentdata ) {
    if( $commentdata['comment_author_url'] == 'Your homepage' )
        $commentdata['comment_author_url'] = '';
    return $commentdata;
}

add_filter('preprocess_comment', 'custom_change_comment_url');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745251939a4618730.html

相关推荐

  • javascript - Comment form validation

    How can I set the validation rules for the comment field?I change the value of commenter namee-mailhomepage onmouseove

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信