regular expression

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

    #1

    regular expression

    hi,

    i intend to validate a string of numbers:

    The conditions are: it can start with a + sign or without. but all must be
    in numbers. a space is allowed

    Consider the following numbers

    +12345922

    12345922

    The above 2 numbers are valid. I've tried the following regexp:

    [\+0-9]$

    It doenst work as intended.

    helloworld12345 67

    goes through!

    whereas

    1234567hellowor ld doesn't!

    please advise




    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    Best Usenet Service Providers 2026 ranked by Newsgroup Access Newsservers, Usenet Search, Features & Free Trial. Add VPN for privacy.

  • Toby A Inkster

    #2
    Re: regular expression

    Eric Layman wrote:
    The conditions are: it can start with a + sign or without. but all must be
    in numbers. a space is allowed
    /^\+?[0-9 ]+$/

    --
    Toby A Inkster BSc (Hons) ARCS
    Contact Me ~ http://tobyinkster.co.uk/contact
    Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

    * = I'm getting there!

    Comment

    • Jeff

      #3
      Re: regular expression

      What your original regular expression is saying is that the last
      character in the string is a + or a number 0 thru 9.

      /^\+?[0-9 ]+$/

      What Toby's expression is saying is that it starts with an optional +
      (? means 0 or 1) and then has 1 or more #'s or spaces in a row until
      the end of the string.

      The ^ means starting at the beginning of the string and the $ means
      the end.

      Hope that helps explain it somewhat.

      Comment

      Working...