-
REGEX in PHP forms
I need a regex expression that verifies if a user inputs a valid name (letters and spaces only). However, it doesn't budge.
Code:
<?php
function validateInputs(){
$status_form = "OK"; $msg="";
$fname=$_GET['fname']; $lname=$_GET['lname'];
$number=$_GET['number']; $subdivision=$_GET['subdivision'];
$street=$_GET['street']; $districtcity=$_GET['districtcity'];
$phonenum=$_GET['phonenum']; $cellnum=$_GET['cellnum'];
if(!preg_match("/[^A-Za-z\s]/i", $lname)){
$status_form="NOTOK";
$msg .= "Name format is invalid. Use only alphabets and/or a dash<br>";
}
if($status_form!="OK"){
while (list ($key, $val) = each ($_POST)) $pd .= "$key=".urlencode($val)."&";
header ("Location: addentry.php?msg=$msg&$pd");
} else return true;
}
if (array_key_exists('add_submit', $_POST)) {
if (validateInputs()){
}
echo "New contact inserted. Details:<br />";
echo "<b>FIRST NAME:</b> {$_POST['fname']}<br />";
echo "<b>LAST NAME:</b> {$_POST['lname']}<br />";
echo "<b>ADDRESS:</b> $addressstring<br />";
echo "<b>MOBILE:</b> {$_POST['phonenum']}<br />";
echo "<b>PHONE:</b> {$_POST['cellnum']}";
} else echo "You can't see this page without submitting the form.";
?>
-
Re: REGEX in PHP forms
It doesn't work because you haven't told it to do anything if a problem is found. Also you have mixed up $_GET and $_POST throughout your code.
-
Re: REGEX in PHP forms
What specific problem? :|
-
Re: REGEX in PHP forms
First off, you are trying to validate it as boolean.
PHP Code:
if (validateInputs()) {
But, no where do you return false if there is a problem in the actual function.
-
Re: REGEX in PHP forms
If it's false, it tries to send it back to the form with the error message attached. It also sends back the values that were filled in.
Code:
while (list ($key, $val) = each ($_POST)) $pd .= "$key=".urlencode($val)."&";
header ("Location: addentry.php?msg=$msg&$pd");
Do I still have to return false for this?
And... how do you make the PHP Codebox? :D
-
Re: REGEX in PHP forms
If the function is supposed to act boolean, as you have it, you must either return true or false.
You make the PHP tags the same way you use the code tags. At the bottom of this page on the left, you will find the "Posting Rules" box. In there, you will find a link to all available BBCode possibilities.
[php]You're PHP code here[/php]