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.
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.
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: