-
Notifications
You must be signed in to change notification settings - Fork 0
Numeric validation class
The numeric validation class tests an input field for numerical values. It works on any text-based input field, but is usually associated with "Text" fields.
To add numeric validation to an input field, you need to include the 'validate' => 'numeric' attribute in a $sections => fields array that allows validation, like so:
<?php
array(
'id' => 'text_demo_numeric',
'type' => 'text',
'title' => __('Text Option - Numeric Validated', Redux_TEXT_DOMAIN),
'sub_desc' => __('This must be numeric.', Redux_TEXT_DOMAIN),
'desc' => __('This is the description field, again good for additional info.', Redux_TEXT_DOMAIN),
'validate' => 'numeric',
'msg' => 'You must provide a numerical value for this option.',
'std' => '0',
),
?>In the above example, a new field of type 'text' is created with 'text_demo'numeric' as its unique ID. By including the 'validate' => 'numeric' attribute, the submitted text will be checked to see if it is numerical. If it isn't a valid number, it will trigger the error handling and display the text included in the 'msg' option. In this example, invalid inputs will display the error You must provide a numerical value for this option., which is also the default error message for numeric validation.
It is a good practice to explain the valid input types using the sub_desc and desc fields so that users understand what is and isn't a valid entry.
To customize the error message that is displayed when non-numerical values are entered, set the 'msg' attribute to whatever text you would like displayed. If this isn't set, the validation class will display a generic message.
When the input text is not valid, the field will be reset to it's current state.
To remove the validation, simply delete the 'validate' => 'numeric' attribute from the array declaration.