Validation Fields
-
Hello i use the CMB2 Tool and try to solve the Problem to Validate 2 Fields.
(The same Question is alsos Here: https://wordpress.stackexchange.com/questions/356633/cmb2-toolkit-compare-2-dates-and-validate-the-time , can i upload here pictures like in wordpress.stackexchange.com ?)Both are fomr type: ‘text_date’, ” and both are working very well. One of them are the Start-Time and the other is the Ending-Time. What i need is to compare both Fields after i click on Publish.
I have wrote this Code for Testing the function:= = = = = = = = = = = = = = =
add_action(‘admin_footer’, ‘event_post_validation’);
function event_post_validation() {
if ($_SERVER[‘PHP_SELF’] != ‘/wp-admin/post-new.php’) {return;}
//here comes the inputs from the user to check
$RNnumber = mt_rand(5, 15);
echo ‘<script>
jQuery( “form#post #publish” ).hide();
jQuery( “form#post #publish” ).after(“<input type=\’button\’ value=\’Publish/Update\’ class=\’sb_publish button-primary\’ /><span class=\’sb_js_errors\’></span>”);
jQuery( “.sb_publish” ).click(function() {
//first problem: after click -> check again the value / call again this function
//second problem: get the userinput and grab them to check the value before it gets updated to the webseite frontend
var error = false
var numberofphp = ‘.$RNnumber.’;
var anumbertest = 99;
if( ‘.$RNnumber.’ <= “8”) {
jQuery(“.sb_js_errors”).text(“less than 8: ‘.$RNnumber.'”);
}
else if( ‘.$RNnumber.’ >= “8”) {
jQuery(“.sb_js_errors”).text(“greater than 8: “+ numberofphp +” “);
}
else if (!error) {
jQuery( “form#post #publish” ).click();
}
else {
jQuery(“.sb_js_errors”).text(“There was an error on the page and therefore this page can not yet be published.”);
}
});
</script>’;
}= = = = = = = = = = = = = = = = = = = = = = = =
This is just a Test because i have there 2 Problems which i can not solve:
First is: When the user writes the wrong Dates, he has to write all Fields again because the click-function triggers just one time. I do not how to solve that.
The second Problem is: How do i get the userinputs without sending them to the Database? I am now realy confused how to solve that like the “types: requried or min” which you made.
I hope i will finish this Task and afterwards you can put the code to the CMB2-Page for other people
Thank you
- The topic ‘Validation Fields’ is closed to new replies.