14 - Regex Validation - WPC
Regex (or regular expression) is a way to check if the user's input is correct. Regex is a language, and you can use PHP Regex with our WooPrice Calculator Plugin (PCRE, click here for more details).
For example, if you want to restrict your user to type only letters in some input fields:
- Click on "PHP Regex"
- Click on "New Regex (PHP)"
- Add a "Name *" for your regex, for example, "Only letters" and write a Regex in the "Regex (PHP)" field, for example: "/^[a-zA-Z]$/" and click on "Save"
- Go to the field on which you want to apply the regex rule, it must be a "Text" field
- You can write the regex rule directly in the "Validation Regex Rule" field or load it from the library by clicking on the cloud icon.
- Write an error message in the "Regex Error Message" field which you want to show in case the regex rule conditions are not met.
As a result:
- If you try to put a number in your website product, you'll get an error message.
- If you try to put only letters, no errors will be shown and the product can be added to the cart successfully after the calculation of price on the backend.