Jump to content

[SOLVED] Allowing alphabetic and numbers only


9three

Recommended Posts

Hey

 

I have this pattern:

 

^([a-zA-Z])|(0-9) ([a-zA-Z])|(0-9)$

 

What I'm trying to accomplish:

 

1. Start with AND end with a alphabetic or number

2. Allow only alphabetic and numbers in the entire string.

 

Did I do it right?

 

Thanks

Link to comment
Share on other sites

When I use the pattern you provided it gives me an error

 

if (!preg_match('^[a-zA-Z0-9]+$', $strUsername))

 

^[a-zA-Z0-9]+$
Warning: preg_match() [function.preg-match]: No ending delimiter '^' found

 

I tried somethings like adding a / at the end but it was no good.

 

:(

Link to comment
Share on other sites

This is because in preg, you need to encase the entire pattern in delimiters... so in your case, you can use:

 

if (!preg_match('#^[a-z0-9]+$#i', $strUsername)) // note the opening and closing #.. I also used the i modifier for case insensitivity

 

Delimiters can be any non alphanumeric, non whitespace ASCII character (except a backslash). You can read up about delimiters here:

 

http://www.phpfreaks.com/forums/index.php/topic,95777.0.html

As well as the pcre portion of the manual.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.