
-----------------------------------
peterggmss
Thu Feb 14, 2008 2:25 pm

Vaidating form input with PHP, for text only field
-----------------------------------
I need to know how to check if a variable contains text.  I need it to contain only text and no numbers, but I can't figure out how with only PHP.

I have
$surname = $_POST["surname"];
$first_name = $_POST["first_name"];
$street_address = $_POST["street_address"];
$phone = $_POST["phone"];

and use
if($surname ==""){
	$error = "You must provide a surname";
}
if($first_name ==""){
	$error = "$error You must provide a first name";
}
if($street_address ==""){
	$error = "$error You must provide a street address";
}
if($phone ==""){
	$error = "$error You must provide a phone number";
}

//Convert phone number to numbers (remove spaces, dashes, periods, etc.)
//Takes numbers with alpha characters into consideration
$phone = str_replace(array(' ', '-', '.', '(', ')', '!', '@', '#', '$', '%', '^', '&', '*', '_', '=', '+'), '', $phone);
if ( strlen($phone) > 10 ){
	$error = "$error Your phone number cannot be more than 10 numbers";
}

if(is_numeric($phone)==0){
	$error="$error The phone number must only contain numbers"; to make sure all fields have a value, and the phone number is only numbers.

But I can't use is_string() for the text fields ($surname and $first_name) because if it contains numbers it will still validate as TRUE (1).

Any advice?
Thanks!

-----------------------------------
md
Thu Feb 14, 2008 3:04 pm

RE:Vaidating form input with PHP, for text only field
-----------------------------------
you could always make a function that steps through a string and makes sure each character contains a letter... 'tis not a very difficult function to write either.

-----------------------------------
PaulButler
Thu Feb 14, 2008 4:17 pm

RE:Vaidating form input with PHP, for text only field
-----------------------------------
Regular expressions will do the trick:


function text_and_spaces_only($str){
return preg_match('/[a-z ]+/i',$str);
}


-----------------------------------
Brightguy
Sun Mar 16, 2008 3:58 am

Re: Vaidating form input with PHP, for text only field
-----------------------------------
ctype_alpha($text) returns true if every character in $text is alphabetic.
