preg_match help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David

    preg_match help

    Help how can I force the variable to start with a character.

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



    function alphanumeric($a lphanumeric_fie ld)
    {
    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!!


  • David

    #2
    Re: preg_match help

    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" <nospam@nospanm .comwrote in message
    news:q9Qbh.659$ HU.278@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($a lphanumeric_fie ld)
    {
    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!!
    >

    Comment

    • Andrew C

      #3
      Re: preg_match help


      "David" <nospam@nospanm .comwrote in message
      news:kmRbh.694$ HU.578@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" <nospam@nospanm .comwrote in message
      news:q9Qbh.659$ HU.278@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($a lphanumeric_fie ld)
      >{
      > 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.


      Comment

      Working...