hosted by liquidweb


Go Back   Web Hosting Talk : Web Hosting Main Forums : Programming Discussion : PHP: String validation
Reply

Forum Jump

PHP: String validation

Reply Post New Thread In Programming Discussion Subscription
 
Send news tip View All Posts Thread Tools Search this Thread Display Modes
  #1  
Old 12-02-2005, 01:55 AM
mylinear mylinear is offline
Junior Guru Wannabe
 
Join Date: Jul 2005
Posts: 77

PHP: String validation


How do I validate whether a string meets the following criteria.
1. Must be 6 to 12 characters in length
2. Must consists of only a to z with no spaces etc

I was using:
$string = "abcdef";
$pattern = "/[a-z]{6,12}/";
preg_match($pattern, $sring);
// returns true

But I found that it would also return true with other strings such as:
$string = " abcdef xyz";

Do I need to include the 3rd array parameter in preg_match and use the resulting array element to do further checking on the match? Or should I not be using preg_match to do this sort of validation?



Sponsored Links
  #2  
Old 12-02-2005, 02:51 AM
mylinear mylinear is offline
Junior Guru Wannabe
 
Join Date: Jul 2005
Posts: 77
Upon further testing, this appears to work.

$pattern = "/^[a-z]{6,12}$/";

But I'm still not sure whether it is the correct way to do this.

  #3  
Old 12-02-2005, 08:37 AM
LH Brandon LH Brandon is offline
Web Hosting Master
 
Join Date: Jan 2005
Location: Olean, NY
Posts: 1,353
Yes, you need to include the ^ and $; otherwise, as long as 6 consecutive letters in the string match the pattern, it's happy, even if other characters do not match. The ^ tells it, though, that the pattern must be first in the string, and $ tells it that the pattern must also be last.

Sponsored Links
  #4  
Old 12-03-2005, 08:02 AM
mylinear mylinear is offline
Junior Guru Wannabe
 
Join Date: Jul 2005
Posts: 77
Thanks to Hostultrix-Brandon for the pointer.

I came across some tutorials and examples on the following site that others may find helpful.

http://www.regularexpressions.info/

Reply

Related posts from TheWhir.com
Title Type Date Posted
Microsoft Validates Parallels Hypervisor Under its Server Virtualization Validation Program Web Hosting News 2013-12-20 11:44:13
SSLGURU Listing 2014-04-24 01:32:23
The Host Group Launches Updated Line of SSL Certificate Options Web Hosting News 2013-06-24 10:14:16
10gen Launches Partner Program to Accelerate Demand for MongoDB Web Hosting News 2013-03-12 14:28:42
DigiCert Launches Extended Validation Code Signing Certificates Web Hosting News 2012-08-15 15:30:28


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes
Postbit Selector

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 Off
HTML code is Off

Forum Jump
Login:
Log in with your username and password
Username:
Password:



Forgot Password?
Advertisement:
Web Hosting News:



 

X

Welcome to WebHostingTalk.com

Create your username to jump into the discussion!

WebHostingTalk.com is the largest, most influentual web hosting community on the Internet. Join us by filling in the form below.


(4 digit year)

Already a member?