Jump to content

Trying To Use Regular Expressions To Validate A Form But My Web Page Isn't Loading.


chdze

Recommended Posts

I trying to use regular expressions to validate my register from, but when i try to check it the internet, i get errors. I skimmed through the code several times but I can't find the error. He's the code:

 

<?php

$name=$_POST['name'];

$address=$_POST['address'];

$city=$_POST['city'];

$phone=$_POST['phone'];

$email=$_POST['email'];

$username=$_POST['username'];

$password=$_POST['password'];

 

if(mysql_connect('***','***','***'))

print "<h2>Thank you for Signing Up!</h2>";

mysql_select_db('Info1');

$query="INSERT INTO UserInfo (Name, Address , City, Phone, Email, Username, Password) VALUES ('$name', '$address', '$city', '$phone', '$email', '$username', '$password')";

 

$pattern_name="^([a-z A-Z] [[:blank]] [a-z A-Z])$";

$string_name=$_POST['name'];

 

$valid_name=ereg($pattern_name, $string_name);

 

$pattern_phone="^([0-9]{3})-([0-9]{4})$";

$string_phone=$_POST['phone'];

 

$valid_phone=ereg($pattern_phone,$string_phone);

 

$pattern_address="^([0-9] [[:blank]] [a-z A-Z])$";

$string_address=$_POST['address'];

 

$valid_address=($pattern_address, $pattern_string);

 

if (!$valid_phone) {

print "Error, Phone number must follow 111-1111 format.";

}

else if (!$valid_name) {

print "Error, Please Enter First and Last name (letters only!)";

}

else if (!$valid_address) {

print "Error, Please Enter your Address (Building number and Street)";

}

else if ($valid_name, valid_phone, valid_address) {

mysql_query($query);

}

 

 

mysql_close();

?>

 

here's the url: http://storm.cis.fordham.edu/~lasbrey/re…

(its nothing fancy, just started learning)

 

thanks.

what do you mean you "get errors"?

ereg() is depreciated, whatever resource your learning from is out of date, you should be using preg_....() instead.

You shouldn't really use a regex on names, unless you plan on checking against all accented characters and the suck, but still, if you want to force someone to use first and last name, use two mandatory fields.

PHP has a better way of validating and sanitizing most form input using filter_vars() and either a validate filter or a sanitise filter.

 

I would suggest you have a long rethink about what using regex validation will do to your form in real terms.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.