Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a custom post type with a bunch of custom fields. I want to validate the meta box input with jQuery as soon as you press the Publish button. Here's what I have (I left out all my validation stuff):

$("input#publish").click(function(e){
    e.preventDefault();
    $("#ajax-loading").show();
    $('form#post').submit();
});

As you can see I am only interrupting the form submit at this point, nothing else going on. However, posts are now saved as drafts only and I can not manually set them to published (I have administrator priviliges). As soon as I remove above code, all is back to normal.

Is there any way to get this type of client side validation working properly without using plugins? I'm on Wordpress 3.6.1.

share|improve this question
    
Check #1705 and #42013 –  brasofilo Oct 23 '13 at 14:38
    
Not really seeing the solution in those posts: the first one describes the same way I do it, and the second one mostly involves server side validation (I am looking for a client side solution). Although in that second post someone does mention the same problem I have with posts being saved as drafts, but no solution is given. –  Marc Oct 23 '13 at 14:47
1  
add comment

2 Answers

up vote 0 down vote accepted

So here's what I ended up doing: I hide the publish button, replace it with my own publish button (one that does not submit the form) and when you click that button the validation script is executed. When there are no errors, a click event is registered on the original publish button. That way, no more drafts and problem solved.

share|improve this answer
add comment

I was having the same problem.

Do not use e.preventDefault();

simple return false or true at the end of your Javascript.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.