PHP validation return false if Email in field value

ontarget

Active Member
Hi
I am trying to validate a field to prevent people from putting their email address in it.
I tried a php validation in both the condition and PHP Code fields:

if (!filter_var($data, FILTER_VALIDATE_EMAIL)) {
return false;
}

In the PHP Code the rule is seemingly ignored and the form just submits despite there being an email address in the field value

Also tried a regex solution:

if (preg_match('/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/',$data)){
return false;
}

With this in the PHP Code the rule is also ignored and the form just submits despite there being an email address in the field value

Does there need to be a condition in order for the PHP code to run? I couldnt see this in the Wiki

for example this doesnt work - it returns the error message field irrespective of the value:
Condition:
if (!filter_var($data, FILTER_VALIDATE_EMAIL)) {
return true;

}
//An email has been found so run return false in PHP code

PHP Code:
return false;
 

Attachments

  • Screenshot 2023-04-19 at 16.03.03.png
    Screenshot 2023-04-19 at 16.03.03.png
    199.1 KB · Views: 46
Not sure what you are doing where.
Which Fabrik version?

PHP Code
Code:
$x = !filter_var($data, FILTER_VALIDATE_EMAIL);
return (bool)$x;
is doing fine (Gamma3)
 
Thanks @troester

Using J4.3.0, F Gamma3, php8.1
Your code works nicely but unfortunately its not triggering a validation error for me.
I did a sanity check with var_dump on all the vars
The issue is as follows
I have a multigroup form.
I have a field called TCN (Teaching Council Number)
Some users have entered their email addresses into this field probably as a result of browser autocomplete.
I want the users to edit /correct their data so i need a php validation on the TCN field to error if they click Next and the field still has an email address in it.
Scenario 1:
TCN Value = me@myemail.com
php Code
$x = !filter_var($data, FILTER_VALIDATE_EMAIL);
return (bool)$x;
false
Show Error message

Scenario 2:
TCN Value = 123XYZ
php Code
$x = !filter_var($data, FILTER_VALIDATE_EMAIL);
return (bool)$x;
true
Allow Next button to be clicked

Unfortunately I cant use isnumeric validation as some numbers have letters in them (go figure!!):rolleyes:
 
What is false and true in your example?

My code has to go into PHP code (not condition) and it's giving a validation error on Save.

Tested also with ajax validation (I don't have a multipage form but it should be the same).
 
Yes I put
PHP:
$x = !filter_var($data, FILTER_VALIDATE_EMAIL);
return (bool)$x;
Into php Code (condition is left empty)
I can still click the next button
If an email is present in the TCN field this is true therefore the form should not be allowed to submit.
Should the php Code therefore be:
PHP:
$x = filter_var($data, FILTER_VALIDATE_EMAIL);
return (bool)$x;
 
filter_var($data, FILTER_VALIDATE_EMAIL);
returns the email (if it's an email, i.e. bool = true) or false, so the validation must take the opposite

I just tested also with a multipage form and it's working as expected.
I don't know what is going on with your element.
Do you have JS errors?
Try without form page break and a simple Save. Is this doing?
 
You could always do something with javascript like this to prevent the autofill:

document.getElementById("TCN Value").setAttribute("autocomplete", "random-string");
document.getElementById("TCN Value").value = "";
 
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top