Skip to content

Instantly share code, notes, and snippets.

@tripflex
Created April 3, 2015 20:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tripflex/66f2eb890d5fc07ff324 to your computer and use it in GitHub Desktop.
Save tripflex/66f2eb890d5fc07ff324 to your computer and use it in GitHub Desktop.
Custom validation for number with decimals
<?php
add_filter('submit_job_form_validate_fields', 'check_price_job_field');
function check_price_job_field( $has_error, $fields, $values ){
// Return true if this field doesn't exist (to prevent errors if you dont have field created)
if( ! isset( $values['job']['price'] ) ) return true;
if( empty( $values['job']['price'] ) || ! is_float( $values['job']['price'] ) ){
throw new Exception( __( 'The custom field value must be numerical with 2 decimal places' ) );
}
// Return true to not have any errors and allow form to continue
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment