Quick Search
Sunbelthost Hosting - Colocation and Dedicated Servers

Threads: 21,763
Posts: 83,205
Members: 24,315
Newest member: joemccue

Web Hosting


 
  #1  
Old 08 Feb 2010, 11:16 AM
Adamg Adamg is offline
Newbie
 
Join Date: Feb 2010
Posts: 2
Form Validation Help

Hey guys,

I need some help with building a form validation function.
I want to make sure a certain field contains only letters, spaces or a hyphen.
I know I can just create an array of legal chars then go over the input and make sure all of its chars are in the legal char array, but that just seems overly complicated running time wise.
Is there a more efficient way oh achieving the same thing?

Thanks!!!

Adam
Reply With Quote
  #2  
Old 08 Feb 2010, 05:41 PM
Alan's Avatar
Alan Alan is offline
Moderator
 
Join Date: Feb 2007
Location: Ireland
Posts: 843
You could use a regular expression (which will probably be slower to process) but will look quite lovely.

PHP Code:
<?php
// Pattern will match any letter (a-z or A-Z), space or "-"
$pattern '/^[a-z -]+$/i';

if(
preg_match($pattern$_POST['example']))
{
    echo 
'Yup, it\'s a match!';
}
else
{
    echo 
'The field contains illegal characters.';
}
?>
I haven't tested it but I'm pretty sure that's right. :S
__________________
“The best thing about a boolean is even if you are wrong, you are only off by a bit.”
Reply With Quote
  #3  
Old 10 Feb 2010, 10:13 AM
Adamg Adamg is offline
Newbie
 
Join Date: Feb 2010
Posts: 2
Thanks very much! This will help immensely!
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
form validation reult with from input itself gauri_agr Client & Server Side Scripting (PHP, ASP, JavaScript) 7 31 Oct 2009 01:44 PM
PHP Form Validation ??? j4mes_bond25 Client & Server Side Scripting (PHP, ASP, JavaScript) 1 25 May 2006 04:17 AM
multi part form validation numbenator Client & Server Side Scripting (PHP, ASP, JavaScript) 1 04 Dec 2005 09:47 AM


All times are GMT -5. The time now is 01:16 PM.


 

Powered by vBulletin® Version 3.8.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Forums Copyright © 2004-2010, WebDevForums.com. Web design by Miami Web Design. All Rights Reserved.