Page 1 of 1

Validation in CGBetterForms

Posted: Mon Apr 15, 2019 4:56 pm
by rooon
Hi all,

In CGBetterForms I want to use validation rules of type
"Match field values against a regular expression"
but somehow my regex are not working.

Code: Select all

<input type="text" name="email">
For example: a@b.c has to result in not valid, and a@bb.nl is valid.
I tried this regex:
/^([a-z0-9_\+\-\.]{1,40})@([a-z0-9\-\.]{2,40})\.(nl|com|org|info|eu)$/gi

Code: Select all

<textarea name="message">
For example: www. in the message should return not valid.
/(http:|https:|ftp:|www|\/\/|\\\\|\.htm|\.php)+/gi
Edit /^(?:(?!http:|https:|ftp:|www|\/\/|\\\\|\.htm|\.php).)+$/gi

How to create the 2 regex strings for these fields?
And what is the proper way to insert a regex in CGBetterForms?

Thanx in advance.

Ronald

Re: Validation in CGBetterForms

Posted: Tue Apr 16, 2019 10:19 am
by rooon
I think it is working with this regex on the input (email)

Code: Select all

<input type="text" name="email">

/^([a-z0-9_\+\-\.]{1,40})@([a-z0-9\-\.]{2,40})\.(nl|com|org|info|eu)$/i
This regex for the textarea (message field) will result in a validation error if there is an url in the textarea and/or the amount of characters is less then 5.

Code: Select all

<textarea name="message">

/^((?!https?:|ftp:|www|\.php|\.html?|[\/\\]{2}).){5,}$/i

Re: Validation in CGBetterForms

Posted: Wed Apr 17, 2019 8:44 pm
by MantaPro
You asked about how to use a regex with CGBetterForms

From the CGBetterForms/Validation tab select the "Validate via Smarty Template" then enter your smarty regex; only output an error message if it fails validation.

Here's an example I used for email validation (on a form that previously suffered a lot of spam; hence the obfuscated field names)

Code: Select all

{* Email -  valid format *}
{if !preg_match('/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/', $fld0130)}
   Invalid fld0130, please correct
{/if}
(my form template uses a smarty array to translate the unfriendly field references to human readable labels

I find this website really useful for regex https://regex101.com/

good luck

[SOLVED] Validation in CGBetterForms

Posted: Sun Apr 21, 2019 1:31 pm
by rooon
MantaPro, thanx for your reply.
At the end it works well with these "Match field values against a regular expression" validation rules:

Code: Select all

<input type="text"> (email from South/West Europe)
match pattern /^([a-z0-9_\+\-\.]{1,40})@([a-z0-9\-\.]{2,40})\.(biz|com|edu|eu|info|int|net|org|be|de|fr|ie|it|lu|nl|at|pt|es|uk|ch)$/i

<input type="text"> (min. 2 characters without leading/trailing spaces)
/^(?=\S+).{2,}\S+$/i

<textarea> (url not allowed)
/^((?!https?:|ftp:|www|\.php|\.htm|\\\\|\/\/).)*$/i

<select> (empty value or '-' not allowed)
/^(?![\s-]*$).+/
I'll keep your template idea in mind for other forms.