Forum Moderators: coopster

Message Too Old, No Replies

If else functions ~~ help. new to php

If else functions

         

indiguy

6:42 am on Sep 9, 2009 (gmt 0)

10+ Year Member



Hi, Im making a validation form. The user should either enter tax, passport or driver licence numbers.
If these three fields are all blank, then dont validate the form.
I want it to be mandatory to enter something into at lease one of them.

Can i do the following? (please note, im still working on the eregi stuff. just wanting to know how to if else a function. there is also name and email, but the following is to see if atlease one of the ID's are inputted and correctly.

PHP CODE:

<?

function validate($TAX, $Driver, $Passport)
{
if($TAX) {
if (eregi("^[0-9]+([0-9])*\\[0-9]{2,4}$",$TAX))
{
$valid = "yes";
}
else
{
$valid = "no";
}
return $valid;
}

else if ($Driver) {
if (eregi("^[a-z]{1,3}([0-9])*\\[0-9]{8,9}$",$Driver))
{
$valid = "yes";
}
else
{
$valid = "no";
}
return $valid;
}

else if ($Passport) {
if (eregi("^[a-z]([-_.]?[a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$",$Passport))
{
$valid = "yes";
}
else
{
$valid = "no";
}
return $valid;

else if (!$TAX, !$Driver, !$Passport){
$valid = "no";
}
}

optik

10:08 am on Sep 9, 2009 (gmt 0)

10+ Year Member



looks fine to me but you may be able to condense the code a bit if you ran the eregi when you make the POST requests for the values then just checked whether your variables were set or not, returning a boolean or string from the function.

indiguy

10:16 am on Sep 9, 2009 (gmt 0)

10+ Year Member



i c, but i have it the way it is, because each area is different. ie. price only to have 0-9
email only to have ^[0-9a-z]([-_.]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$.. indicating its for email, but name input doesnt need @ or numbers. so thats why there seems heaps of code :)

omoutop

2:21 pm on Sep 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



don't you mind that your function will validate with one of the fields only?
if i read your if..else correclty, your function will validate yes/no by using only the tax field... it will not go further down to validate the other 2 fields
am i correct or did i miss something?

CyBerAliEn

5:19 pm on Sep 9, 2009 (gmt 0)

10+ Year Member



omoutop has a point. Because you have one function... with multiple 'return' points; you have a dilemma! As soon as PHP returns a value from the function, it will exit the function!

In short, the design of your function is flawed, in that it will not operate as you intend or expect. I would suggest the following to replace your function:

<?php
function validate($type,$string)
{
$output = true;
$patterns = array();
$patterns['TAX'] = "^[0-9]+([0-9])*\\[0-9]{2,4}$";
$patterns['Driver'] = "^[a-z]{1,3}([0-9])*\\[0-9]{8,9}$";
$patterns['Passport'] = "^[a-z]([-_.]?[a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$";
$usePattern = $patterns[$type];
if ($usePattern!="") { if (eregi($usePattern,$string)===false) { $output = false; } }
return $output;
}

//Then, run this function to validate each string, such as:
$validTAX = validate('TAX',$TAX);
$validDriver = validate('Driver',$Driver);
$validPassport = validate('Passport',$Passport);

//these 'valid' variables will then contain boolean true/false
//as to whether or not the items are valid

//You could also create a wrapper function, that does the work
//for you, such as 'validateFormNumbers($TAX,$Driver,$Passport)'
//then have this function do code like the above example,
//such as: validate('someKey',$someString)
?>

You could then use this function to validate other number types by simply adding new entries to the array 'patterns' and calling the function appropriately. I have not tested the function, but it should work.

EDIT: Noticed rocknbil posted about the time I did, lol. I would follow his suggestion about not using the eregi function, and appropriately fixing the actual patterns as he notes. However... depending on what exactly you want to have achieved, his func or mine may or may not better. Best of luck!

[edited by: CyBerAliEn at 5:29 pm (utc) on Sep. 9, 2009]

rocknbil

5:24 pm on Sep 9, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A couple recommendations.

You don't need a yes or no value to validate something. Use 0 or 1, or just leave it NULL and if defined it validates, otherwise it doesn't.

This allows you to only set the variable if it validates, eliminating the multiple return statements.

do not use eregi, use preg_match instead. Note that it has been deprecated [us.php.net].

A quick revise of your code, please test for errors as I just typed it out:


<?
function validate($TAX, $Driver, $Passport) {
var $valid=NULL;
if ($TAX and preg_match('/^[0-9]+\\[0-9]{2,4}$/',$TAX)) {
$valid = 1;
}
// Note that if you want DRIVER or PASSPORT to
// overwrite TAX validation, don't use else if. Use if.
else if ($Driver and preg_match('/^[a-z]{1,3}([0-9])*\\[0-9]{8,9}$/i',$Driver)) {
$valid = 1;
}
// Ditto here, if, or else if?
else if ($Passport and
preg_match('/^[a-z][-_.]?[a-z]*@[0-9a-z][-.]?[0-9a-z]*\\.[a-z]{2,4}$/i',$Passport)) {
$valid = 1;
}
// It will be NULL or 1.
return $valid;
}
?>

Some explanations of my changes to your regexps:

- [0-9]+([0-9])*\\[0-9]{2,4}
This says "one or more of 0-9 next to zero or more of 0-9 next to a backslash next to 2-4 0-9's." The () means "save and store in $1" which you're not using. I don't know what you're trying to do here. Give us a sample target string.

- ^[a-z]{1,3}([0-9])*\\[0-9]{8,9}$
"1-3 lower case letters next to zero or more numbers next to a backslash next to 8 or 9 numbers, store () this in $1." Again, not using $1, and you don't have a case insensitive modifier so ABC would fail.

- ^[a-z]([-_.]?[a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$
I **REALLY** don't know what you're doing here, sample string required, added only case-insensitive modifer and again, parentheses in regexp have a different meaning, it's not just grouping.

indiguy

1:47 am on Sep 10, 2009 (gmt 0)

10+ Year Member



sample string:

Enter TAx:__________________ or
Enter passport:__________________ or

-------------- php
<input class="contact" type="text" name="IRD" size="10"/><b> or</b><br/><br/>
<b>Drivers Licence</b>: <input class="contact" type="text" name="Driver" size="10"/><b> or</b>

rocknbil

1:52 am on Sep 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, what I meant was

Sample U.S. social security string:

123-45-6789
or
123456789
or
123 45 6789

Sample U.S. phone string:

123-456-7890
or
123.456.7890
or
123 456 7890
or
1234567890

By knowing the nature of the allowed formats for specific fields, it helps to build the regexps.

indiguy

2:47 am on Sep 10, 2009 (gmt 0)

10+ Year Member



thanks, i understand:
//this is to validate phone number:

function validate($phone)
{
var $valid=NULL;
// format: 2-4 numbers then a '-' 3-4 numbers then a '-' 3-4
if (preg_match('/^[0-9]{2,4}*\-[0-9]{3,4}*\-[0-9]{3,4}$/',$phone))
{
$valid = 1;
}
return $valid;
}

within html form:
<b>Phone</b>: <input class="contact" type="text" name="phone" size="15"/><b> *</b><br/><br/>
<b>Examples: 012-123-1234 ¦ 0800-1234-1234 ¦ 07-123-1234</b>

rocknbil

6:22 pm on Sep 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Okay, using that as an example, there may be some errors in your logic, or maybe this is what you intend. See my previous example for phones. Do you want to kick it if they enter dots, or spaces, or nothing at all? If this is your intent, fine, but remember that if you annoy a user enough, and they don't "get it," you lose them. You can open up your validation by allowing dots, spaces, or no delimiter. Also remember \d is the equivalent of 0-9.

// If you want area code, ., space, or - delimiter optional, keep in mind they may accidentally have a space on one end or the other:

NOTE: all regexps below may contain errors, typed out for demonstration

'/^\s*\d{2,4}[\-\.\s]*\d{3,4}[\-\.\s]*\d{3,4}\s*$/'

In the U.S., there is no need for 3 or 4:

'/^\s*\d{3}[\-\.\s]*\d{3}[\-\.\s]*\d{4}\s*$/'

If the delimiter is mandatory, but can still be a dot or dash, no space:

'/^\s*\d{3}[\-\.]+\d{4}[\-\.]+\d{4}\s*$/'

You should always do as much work for them as you can. So, for example, if you MUST have it formatted with dashes, go ahead an allow the dots, dashes, or spaces as above, but fix it:

$phone = preg_replace('/^\s*(\d{3})[\-\.\s]*(\d{3})[\-\.\s]*(\d{4})\s*$/',"$1\-$2\-$3",$phone);

You now see the application of () and $1, $2, $3 in action.

A few notes:
* means zero or more, making this part of the pattern optional.
+ means one or more.
{3,4} means between 3 and 4 of the previous pattern.
{3} means ONLY 3 of the previous pattern.
? after any expression (\d+?)is a quantifier and limits the match to the first instance found, preventing it from slurping up the whole expression. Not used here, but may have to in some cases.
() saves the sub-pattern in numbered variables; the first from left to right in the pattern is $1, the second is $2, and so on. This is only needed in a replace/substitution function, never will be needed in a simple match UNLESS you do something like this.

$string = 'my car';
if (preg_match('/(car)/',$string)) {
$string = $1;
}
echo "$string"; // should be 'car'

However this is an unreliable way to use these, but generally will work.

Returning to the original problem, sample strings for $TAX, $Driver, and $Passport in your initial function can help build regexps for those.