CMS Made Simple Forums
https://forum.cmsmadesimple.org/

Validation in CGBetterForms
https://forum.cmsmadesimple.org/viewtopic.php?f=7&t=80621
Page 1 of 1

Author:  rooon [ Mon Apr 15, 2019 4:56 pm ]
Post subject:  Validation in CGBetterForms

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

Author:  rooon [ Tue Apr 16, 2019 10:19 am ]
Post subject:  Re: Validation in CGBetterForms

I think it is working with this regex on the input (email)
Code:
<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:
<textarea name="message">

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

Author:  MantaPro [ Wed Apr 17, 2019 8:44 pm ]
Post subject:  Re: Validation in CGBetterForms

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:
{* 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

Author:  rooon [ Sun Apr 21, 2019 1:31 pm ]
Post subject:  [SOLVED] Validation in CGBetterForms

MantaPro, thanx for your reply.
At the end it works well with these "Match field values against a regular expression" validation rules:
Code:
<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.

Page 1 of 1 All times are UTC
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/