I have a post edit form which collects user data for a custom field. Now I want a form validation so that the user input value is not empty and less than 5. How can I do that?
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($field_data, $form_data) {
//field data are field values and errors
list($fields,$errors)=$field_data;
//validate if specific form
if ($form_data['id']==99999) {
if (trim($fields['wpcf-book']['value']) == '' or $fields['wpcf-book']['value'] > 6){
$errors['wpcf-book']='Please enter less than or equal to 5.';
}
}
//return result
return array($fields,$errors);
}
Where:
- Replace 99999 with your original form ID
- Replace the book with your original field slug.
You are welcome to adjust the code as required if needed.
Hi, thanks for your reply. When an user provides a value grater than 5 or a null value and submits the form, I want to show the error message below the form. How can I do that ?