Forum Moderators: coopster
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";
}
}
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]
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;
}
?>
- [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.
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>
// 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.