Validating a form field...

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
carlosg
Forum Newbie
Posts: 2
Joined: Fri Aug 19, 2005 11:27 am

Validating a form field...

Post by carlosg »

Hi there,
I am presently facing a bit of an issue when someone enters their data in my form. The issue is that the form will only allow the user to enter a zip code (allowing numbers only) and what I need from it is to allow entries for both Zip Code (90210) and Postal Code for instance M1M 2N5.

Here is the code :

Please note :

Code: Select all

if (!valid_number($userzip)) 
         $err = $err."• Postal code<br>";

Code: Select all

<?
#=========== check if the form is filled out =============#

$err= "";

	if (isset($register))
	{
		if ($username=="")
			$err = $err."• Username<br>";
		if ($userpass=="")
			$err = $err."• Password<br>";
		if ($userfullname=="")
			$err = $err."• Name<br>";
		if (!valid_email($useremail))
			$err = $err."• E-mail<br>";
		if ($useraddress1=="" && $useraddress2=="")
			$err = $err."• Address<br>";
		if ($usercity=="")
			$err = $err."• City<br>";
		if ($userstate=="")
			$err = $err."• State/province<br>";
		if (!valid_number($userzip))
			$err = $err."• Postal code<br>";
		if ($terms=="")
			$err = $err."• You should agree with terms of the site!<br>";
		
		if (valid_email($useremail) && $escrowid=="")
		{
			$db->query("select id from ".prefix."users where useremail='$useremail'");
			$db->next_record();
				if ($db->f("id")!="")
					$err = $err."• An account already exists with this email address!<br>";
		}
		
		if ($username!="")
		{
			$db->query("select id from ".prefix."users where username='$username'");
			$db->next_record();
				if ($db->f("id")!="")
					$err = $err."• Username has been taken. Please select a different username!<br>";
		}
		
		if ($err=="" && $escrowid=="")
		{
			$db->query("insert into ".prefix."users (username, userpass, userfullname, useremail, userbusiness, useraddress1, useraddress2, usercity, userstate, userzip, usercountry, paymentaccepttype, registrationdate) 
						values ('$username', '$userpass', '$userfullname', '$useremail', '$userbusiness', '$useraddress1', '$useraddress2', '$usercity', '$userstate', '$userzip', '$usercountry', '$paymentaccepttype', '".date("U")."')");
			echo "Thank you for the registration with us.";
			
			registration_letter($useremail);
			
			include("inc/footer.php");
			exit();
		}
	}
The function it refers to is :

Code: Select all

function valid_number($num)
{
	if(ereg("^[0-9]+$",$num))
		return true;
	else
		return false;
}
Now, I am not a php programmer but I presume the above statement "if(ereg("^[0-9]+$",$num))" should be changed to reflect Alpha-Numerical values.

If anyone could kindly help me I will be eternally greatfull.

Thanks in advance.
Last edited by carlosg on Fri Aug 19, 2005 1:30 pm, edited 1 time in total.
korto
Forum Commoner
Posts: 36
Joined: Thu Aug 18, 2005 6:30 am
Location: Greece
Contact:

Post by korto »

I am of the opinion that you didn't have to post all the code...
Now,do you want to allow numbers as well as chars at the zip input element? What do you want to rule out,special chars, dashes etc?
carlosg
Forum Newbie
Posts: 2
Joined: Fri Aug 19, 2005 11:27 am

Sorry, I did not mean to enter the entire code, I will edit.

Post by carlosg »

korto wrote:I am of the opinion that you didn't have to post all the code...
Now,do you want to allow numbers as well as chars at the zip input element? What do you want to rule out,special chars, dashes etc?
Sorry, you are absolutely right... I thought of only including the relevant code but also thought it wouldmake more sense for someone to view the entire thing.

All I need is to not allow Characters such as (!@#$%^&*).... Because I am in Canada we use Postal Codes but I also need my users in the US to be able to enter their Zip Codes. :D

Thanks Korto
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

code corrected!!!

Post by raghavan20 »

hope this helps :)

Code: Select all

$str5 = "sfd23dsdf";
$str6 = "sdf@#$dsf";
echo "pattern match:".preg_match("/[\!\@\#\$\%\^\&\*]+/", $str5);//returns 0
echo "pattern match:".preg_match("/[\!\@\#\$\%\^\&\*]+/", $str6);//returns 1
Last edited by raghavan20 on Fri Aug 19, 2005 5:04 pm, edited 2 times in total.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

hope these might be helpful as well
for Canada
/([a-zA-z][0-9]){3}/
/[a-zA-z][0-9][a-zA-z]\s?[0-9][a-zA-z][0-9]/
for US
/[1-9][0-9]{4}/



User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

raghavan20 wrote:hope these might be helpful as well
for Canada
/([a-zA-z][0-9]){3}/
/[a-zA-z][0-9][a-zA-z]\s?[0-9][a-zA-z][0-9]/
for US
/[1-9][0-9]{4}/
Just so you know, 0 is a valid first character of a zip-code.... Most of New England has it (or maybe you wanted to exclude us :) )
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

do you say that '0' is a valid first character in US zip code??? :roll:
common england format:
st4 2bq
st43 2ez
london post codes are different, they hv got a different set. london is actually different from all other places :wink:
you can say mostly from London post codes, which part of london is it. :)
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

raghavan20 wrote:do you say that '0' is a valid first character in US zip code??? :roll:
Yes '0' is a valid first character of a US zip code. Your regexp wouldn't match any of Massachusetts (02[0-9]{#}) or New Hampsihre(03[0-9]{3}, etc. don't know the others offhand.
korto
Forum Commoner
Posts: 36
Joined: Thu Aug 18, 2005 6:30 am
Location: Greece
Contact:

Post by korto »

I suppose one could also use a javascript function that would validate the input by parsing each character, if regexp is not your flavor
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you still require some validator on the server side.. regex works well for this..
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

carlosg wrote:Thanks for the tips raghavan20, nielsene, korto and feyd, you guys Rock!.
I definitely want to include New England :).

I have made the changes to my code and all is sweet.

Thanks once more.

Cheers, Carlos

mistakenly posted to a new thread....
Post Reply