This topic is locked
[SOLVED]

 How to use preg_match function in if statement?

8/30/2013 1:48:07 AM
PHPRunner General questions
P
phpcmk author

Hi,
I need to prompt user if the nationality selected is not an Singaporean/PR-xxx, a levy column cannot be 0.
By default I have set levy to 0, so I tried to add the following code in "Before record updated":
As there can be many PR-xxx citizen, thus I tried using preg-match.



if (($values["nationality"]!="Singaporean" || $values["nationality"]!=preg_match("[PR-]",$values["nationality"])) && $values["foreignlevy"]==0){

$message="Levy cannot be 0 unless nationality is Singaporean / PR-xxx";

return false;

}


The code seems not working, can someone kindly advise how can I check for the mentioned condition? Thanks.

C
cgphp 8/30/2013

preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.

$match = preg_match("[PR-]",$values["nationality"]);
if (($values["nationality"] != "Singaporean" || $match === 0 || $match === FALSE) && $values["foreignlevy"]==0){

$message="Levy cannot be 0 unless nationality is Singaporean / PR-xxx";

return false;

}


Make sure the [PR-] pattern is correct.

P
phpcmk author 9/1/2013



preg_match() returns 1 if the pattern matches given subject, 0 if it does not, or FALSE if an error occurred.

$match = preg_match("[PR-]",$values["nationality"]);
if (($values["nationality"] != "Singaporean" || $match === 0 || $match === FALSE) && $values["foreignlevy"]==0){

$message="Levy cannot be 0 unless nationality is Singaporean / PR-xxx";

return false;

}


Make sure the [PR-] pattern is correct.


Hi,
I have tried to implement the code but it seems not working. When I select any of the following drop-down option with levy 0, I will receive the message.
By right, I should receive the message only when not selecting Singaporean/PR-xxx with levy 0.
My drop-down option includes:

  • Singaporean
  • PR-Malaysian
  • Indonesian
    When I removed the following code, I can successfully submit the form if I select as Singaporean with levy 0.
    $match === 0 || $match === FALSE
    Can kindly advise? Thanks.

C
cgphp 9/2/2013

I think you don't need the preg_match check here. You have three fixed values: Singaporean, PR-Malaysian and Indonesian. Make check control against these values.

P
phpcmk author 9/3/2013



I think you don't need the preg_match check here. You have three fixed values: Singaporean, PR-Malaysian and Indonesian. Make check control against these values.


Sorry for not stating properly.
Actually, the 3 values are only examples. I have a lot of nationality (maybe 20 or more).
I have created a nationality tab for the administrator to freely add new nationality and when creating a form, it will reflect in the master list as the drop-down option in the nationality column.
Thus, I have no idea how many nationality there will be.
Not very sure how to make check control in this case? Any advise? Thanks.

C
cgphp 9/3/2013

Check this solution:

$match = preg_match("/^PR-/",$values["nationality"]);
if (($values["nationality"] != "Singaporean" && ($match === 0 || $match === FALSE)) && $values["foreignlevy"]==0){

$message="Levy cannot be 0 unless nationality is Singaporean / PR-xxx";

return false;

}