Jump to content
Larry Ullman's Book Forums

Recommended Posts

I used this regular expression in a preg_match, now a value 9 would not pass it neither would 99 or 999, but 0 will go through it. How is that, although I am only accepting A-Z \'.- with a range of 2 - 50?

 

preg_match ('/^[A-Z \'.-]{2,50}$/i', $trimmed['data'])

Link to comment
Share on other sites

Hmmm... I'm not sure, but maybe the value stored in $trimmed['data'] isn't what you expected.

I just ran the following tests with the following results (in comments):

 

<?php

echo preg_match('/^[A-Z \'.-]{2,50}$/i', '0'); // Echos 0.

?>

 

 

<?php

echo preg_match('/^[A-Z \'.-]{2,50}$/i', '99'); // Echos 0.

?>

 

 

<?php

echo preg_match('/^[A-Z \'.-]{2,50}$/i', 'AS'); // Echos 1.

?>

 

 

<?php

echo preg_match('/^[A-Z \'.-]{2,50}$/i', 'AS45'); // Echos 0.

?>

 

 

<?php

echo preg_match('/^[A-Z \'.-]{2,50}$/i', 'A.'); // Echos 1.

?>

 

In other words, it seems to be working as expected.

For some reason, I think the second argument in the function is not what you expect it to be (or something).

  • Upvote 1
Link to comment
Share on other sites

If spaces are valid characters, then yes, I'd trim the string after the regex. The alternative is to trim the string first and not allow for the first and last characters to be spaces.

 

For example, your regex now would allow for " " (two spaces), which would be trimmed down to nothing after the trim function; probably not what you want.

Link to comment
Share on other sites

Because I answer most of them while I'm at work.

I have a wife and 3-year-old daughter at home with a second kid on the way, so I rarely get any time at home to get online and check out the forum. At work, however, because I work very efficiently, I often get enough time to answer questions from work.

 

The downside of answering questions at work is that I'm stuck using IE6 at work, so I can't test HTML5-exclusive features and I'm forced to use my least-favorite browser all the time in order to do research and test out scripts for answering questions on this forum.

All in all though, I've found that it's a good way to break up my workday so that I don't tire of my work as easily as some of my co-workers.

 

Just like how the engineers at Google take hammock breaks, I take Larry Ullman Forum breaks; I enjoy trying to answer as many questions as I can (without getting too greedy) because it keeps me learning and sharp.

  • Upvote 1
Link to comment
Share on other sites

Right now I understand, i also want to take part in answering the questions but now i am tired after my own project work and need to take a break from looking at code. I agree with you on that the fact that answering all the questions can keep you sharpened up. I guess you will be able to pass all your web knowledge onto your kids.

 

Okay i have done a bit more work, this was my code:

 

// Check for valid province:

if (!empty($_POST['province'])) {

if (preg_match ('/^[A-Z \'.-]{2,50}$/i', $_POST['province'])) {

// Save info for database

} else {

$reg_errors['province'] = 'Please enter a valid province';

}

}

 

Now i found that empty will be true if an integer value of zero is entered, you see this form field isn't required so i am only running the validation on it if a value is entered. Anyhow i am going to the value converted to a string before i run it through the empty then onto pregmatch that way it will work.

Link to comment
Share on other sites

Sounds like a plan.

Glad you got it all working.

 

Also, yeah, of course I get tired too sometimes (errr... a lot of the time) and I don't want to respond to the various queries on the boards.

That's life, but just respond when you can. That's what I do.

Link to comment
Share on other sites

I have tried to use strval below before adding it into the empty() function, it still doesn't work, so it seems that empty reads a 0 as either an interger or a string as still a FALSE. I couldn't find a typecast for strings so now i think the only solution is using a conditional here.

 

strval — Get string value of a variable

 

Report a bug

notes-reject.gif Description

 

string strval ( mixed $var )

Get the string value of a variable. See the documentation on string for more information on converting to string.

This function performs no formatting on the returned value. If you are looking for a way to format a numeric value as a string, please see sprintf() or number_format().

Link to comment
Share on other sites

Okay i got it to work with this

 

// Check for valid province:

if (!empty($_POST['province']) && $_POST['province']=='0') {

if (preg_match ('/^[A-Z \'.-]{2,50}$/i', $_POST['province'])) {

// Save info for database

} else {

$reg_errors['province'] = 'Please enter a valid province';

}

}

Link to comment
Share on other sites

 Share

×
×
  • Create New...