Adding a field that accepts only numeric characters
In this recipe, we will add a rule that accepts only numeric characters, that is, digits 0-9. This could be useful, for example, when the user is required to enter a reference number or a currency value.
Getting ready
Please refer to the first recipe in this chapter for details on how to prepare a QuickForm web form which is the basis of this recipe.
How to do it...
Add the following code to our form definition in validation_form.php
, just after the field definition:
$mform->addElement('text', 'mytext9', 'Numeric'); $mform->addRule('mytext9', 'Numeric', 'numeric', null, 'client');
When we test out this new rule by entering non-numeric characters (in this example, we entered the string abc), we see that our validation warning message is displayed and we are unable to submit the form as seen in the following screenshot:
How it works...
We used the validation type numeric
which does not require any format options. Now when a user enters...