Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Vaidating form input with PHP, for text only field
Index -> Programming, PHP -> PHP Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
peterggmss




PostPosted: Thu Feb 14, 2008 2:25 pm   Post subject: 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
code:
$surname = $_POST["surname"];
$first_name = $_POST["first_name"];
$street_address = $_POST["street_address"];
$phone = $_POST["phone"];


and use
code:
if($surname ==""){
        $error = "You must provide a surname";
}
if($first_name ==""){
        $error = "$error <br>You must provide a first name";
}
if($street_address ==""){
        $error = "$error <br>You must provide a street address";
}
if($phone ==""){
        $error = "$error <br>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 <br>Your phone number cannot be more than 10 numbers";
}

if(is_numeric($phone)==0){
        $error="$error <br>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!
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Thu Feb 14, 2008 3:04 pm   Post subject: 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




PostPosted: Thu Feb 14, 2008 4:17 pm   Post subject: RE:Vaidating form input with PHP, for text only field

Regular expressions will do the trick:

code:

function text_and_spaces_only($str){
return preg_match('/[a-z ]+/i',$str);
}
Brightguy




PostPosted: Sun Mar 16, 2008 3:58 am   Post subject: Re: Vaidating form input with PHP, for text only field

ctype_alpha($text) returns true if every character in $text is alphabetic.
Display posts from previous:   
   Index -> Programming, PHP -> PHP Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: