Page 1 of 1

Regular Expression Question

Posted: Tue Sep 17, 2002 11:33 am
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

Posted: Tue Sep 17, 2002 12:03 pm
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.

Posted: Tue Sep 17, 2002 12:06 pm
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.

Posted: Tue Sep 17, 2002 12:11 pm
by mr_griff
Is this what you are looking for??

Code: Select all

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

Posted: Tue Sep 17, 2002 12:27 pm
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?

Posted: Tue Sep 17, 2002 12:34 pm
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'])) 

?>