Regular Expression Question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Regular Expression Question

Post by JPlush76 »

I'm validating an address field and I have a regular expression question. Basically I want them to be able to enter "555 maple ave"

and I have this regexp but I was wondering if there was an easier way of doing it.

Code: Select all

<?php
// Check the address 1 field.	
	if (eregi ("^їa-z0-9]*її]]*їa-z0-9]*її]]*їa-z0-9]*$", $_POSTї'f_add1'])) {
		$e = TRUE;
	} else {
		$e = FALSE;
		$messageї] = "Please enter an address1 that consists only of letters and numbers.";
	}
?>
basically I want them to enter anything they want as long as its alpha numeric and/or has a period

so...

Maple Ave. IS OK

766 Oak St IS OK

894 slkj////sdlkj'; IS NOT OK
User avatar
mr_griff
Forum Commoner
Posts: 64
Joined: Tue Sep 17, 2002 11:11 am
Location: Bozeman, Montana

Post by mr_griff »

Are you going to be accepting foreign orders? A lot of foreign addresses or PO boxes don't follow your format. Also what if someone has an address like "123 main street NE". You might want to just check to make sure they filled something out.
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

yep thats why I posted Griff, once I started thinking about what's going in that field my expression could be 10 miles long and right now you're correct its not a practicaly use.

we will be accepting foreign orders.

I was just wondering if there was a good way to check to make sure the field only contains letters, numbers, spaces, and periods.

I'm just looking for a way to make my forms a little more secure, just checking to see if the form is filled in leaves you open to alot of possibilities.
User avatar
mr_griff
Forum Commoner
Posts: 64
Joined: Tue Sep 17, 2002 11:11 am
Location: Bozeman, Montana

Post by mr_griff »

Is this what you are looking for??

Code: Select all

<?php
if (eregi("^їa-z0-9 \.]{1,}$", $_POSTї'f_add1']))
?>
JPlush76
Forum Regular
Posts: 819
Joined: Thu Aug 01, 2002 5:42 pm
Location: Los Angeles, CA
Contact:

Post by JPlush76 »

that looks like its working exactly like I need, thanks!

so basically that says I have have any letter or number or space or period and it repeats 1 or more times?
User avatar
mr_griff
Forum Commoner
Posts: 64
Joined: Tue Sep 17, 2002 11:11 am
Location: Bozeman, Montana

Post by mr_griff »

Yep??

You could also make the user have letter or number first then spaces and periods by doing this:

Code: Select all

<?php

if (eregi("^їa-z0-9]{1}їa-z0-9 \.]{0,}$", $_POSTї'f_add1'])) 

?>
Post Reply