By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,645 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,645 IT Pros & Developers. It's quick & easy.

preg_match help

P: n/a
Help how can I force the variable to start with a character.

I dont want it to start with a number????

function alphanumeric($alphanumeric_field)
{
if(!preg_match("/[^a-zA-Z0-9]+$/",$alphanumeric_field))
return TRUE;
else
return FALSE;
}

any ideas much appreciated. As i am so frustrated with the preg_match
function!!
Dec 1 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
I forgot to mention as well that it was allowing whitespace. Which i did not
want.

all i want is A-Z regardless of case. and Numbers. No other characters. And
start with a letter.


"David" <no****@nospanm.comwrote in message
news:q9**************@news-server.bigpond.net.au...
Help how can I force the variable to start with a character.

I dont want it to start with a number????

function alphanumeric($alphanumeric_field)
{
if(!preg_match("/[^a-zA-Z0-9]+$/",$alphanumeric_field))
return TRUE;
else
return FALSE;
}

any ideas much appreciated. As i am so frustrated with the preg_match
function!!

Dec 1 '06 #2

P: n/a

"David" <no****@nospanm.comwrote in message
news:km**************@news-server.bigpond.net.au...
>I forgot to mention as well that it was allowing whitespace. Which i did
not
want.

all i want is A-Z regardless of case. and Numbers. No other characters.
And
start with a letter.


"David" <no****@nospanm.comwrote in message
news:q9**************@news-server.bigpond.net.au...
>Help how can I force the variable to start with a character.

I dont want it to start with a number????

function alphanumeric($alphanumeric_field)
{
if(!preg_match("/[^a-zA-Z0-9]+$/",$alphanumeric_field))
return TRUE;
else
return FALSE;
}

any ideas much appreciated. As i am so frustrated with the preg_match
function!!
I haven't tested this, but you probably want something along these lines:

/^[A-Za-z][A-Za-z0-9]*$/

A '^' (carat) inside of '[]' means NOT (so, '[^A-Z]' means any single
character EXCEPT an upper-case letter). A '^' outside of '[]' means start of
line.

The above pattern, then, is looking for...

Start of line THEN a single alphabetic character (upper- or lower-case) THEN
zero or more alphabetic characters (upper- or lower-case) or decimal digits
THEN end of line (the '$' means end of line).

That's it really.

A.
Dec 1 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.