Re: [PHP] One last try at this!



Jim Lucas wrote:
Beauford wrote:
This is what I am trying to do, and for the most part it works, but it also
may be causing some of my other problems.

If $event contains invalid characters, I am returning a string that says
"Invalid Characters" So $result now contains the value 'Invalid Characters'.
Then $formerror['event'] = $result - is self explanatory.

If there are no errors nothing is returned and $result should contain
anything. But this doesn't always seem to be the case. This is one of the
problems I am having.

This is the code in a nutshell.

if($result = ValidateString($event)) { $formerror['event'] = $result;
function ValidateString($string) {

if(!preg_match("/^[-A-Za-z0-9_.' ]+$/", $string)) {
return "Invalid Characters";
} Thanks

In your regex you have a "." this will match anything

try this:

<plaintext><?php

function ValidateString($string) {
if ( preg_match("/[^a-zA-Z0-9\_\.\' -]+/", $string) ) {
return "Invalid Characters";
}
return false;
}

$formerror = array();
forgot to say that all you need to do to get rid of the default values is remove the following two lines

$formerror['firstAttempt'] = 'first attempt';
$formerror['secondAttempt'] = 'second attempt';

$event = "A-Z and a-z plus 0-9 and an '_' are allowed.";
if ( ( $result = ValidateString($event) ) !== false ) {
$formerror['firstAttempt'] = $result;
}

$event = "But bad chars like !@#$ and %^&* are not allowed.";
if ( ( $result = ValidateString($event) ) !== false ) {
$formerror['secondAttempt'] = $result;
}

var_dump($formerror);

?>

Jim Lucas



-----Original Message-----
From: Jim Lucas [mailto:lists@xxxxxxxxx] Sent: January 17, 2007 4:36 PM
To: Jay Blanchard
Cc: Beauford; PHP
Subject: Re: [PHP] One last try at this!

Jay Blanchard wrote:
[snip]
The second condition of each if statement does not contain
equality
checking, it sets the $result to ValidateString($event, "2"). That should be if($result == ValidateString($event, "2")) or if($result === ValidateString($event, "2"))

What if the intension was to fail if the result of ValidateString() was false??

Then it should be writen as such.

if ( ( $result = ValidateString($event, "2") ) !== FALSE ) {
$formerror['event'] = $result;
}
[/snip]

Hmmm.....did you test that?
$result = ValidateString($event, "2") is always TRUE,
$result has had
something assigned to it unless the function ValidateString
returns a
FALSE. The OP never provided the code for the function so we just don't know. Also, the OP forgot to run subsequent conditions in an elseif check, instead he used 3 if statements that will be
executed each time.
At one point he mentioned changing ValidateString() to return false on an error.

At least that is what I thought he said.

--
PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php





.



Relevant Pages

  • RE: Post Code UK Structure Verification
    ... Dim invalid As Boolean ... ' Validate inner code ..... ... Notepad is used because copying from ... Sort to bring to the top records with too many characters ...
    (microsoft.public.excel.programming)
  • Re: trouble joining exchange 5.5 domain
    ... There might be some old data in AD that references the invalid domain name. ... A SKCC might be required. ... > message when trying to add an exchange 2000 server to the ... > greater than 64 characters or contains at least one of the ...
    (microsoft.public.exchange.misc)
  • Re: mail address validation
    ... [I'm unsure whether the quoting levels are correct. ... what characters are forbidden in "domain" part ... I have read this but regexp on this page return true for user@domain By my opinion this is invalid mail address because TLD ...
    (comp.lang.perl.misc)
  • Re: Invalid character in XML
    ... Assuming that the characters are invalid not because of the wrong encoding ... There may be some non-standard option on the XML ... While I'm reading in the data I get an error. ...
    (microsoft.public.sqlserver.xml)
  • Re: [PHP] One last try at this!
    ... "Invalid Characters" So $result now contains the value 'Invalid Characters'. ... At one point he mentioned changing ValidateString() to return false on an error. ... PHP General Mailing List To unsubscribe, ...
    (php.general)