SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    May 2004
    Location
    aa
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question regular expression pls

    Hi, all

    i was struggling to write a simple regular expression but i failed..

    so came to forum..

    pls do me a faour.

    what i need is..

    1. a simple regular expression which checks for input string for
    a. it should be alphanumeric only and space is allowed
    b. but it should not be just a number.. (i.e is should contain atleast one character)

    you can use the following link for checking your expression..

    http://www.nvcc.edu/home/drodgers/ce...est_regexp.asp


    please do help me out.

    thanx

  2. #2
    SitePoint Guru momos's Avatar
    Join Date
    Apr 2004
    Location
    Belgium
    Posts
    920
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    \w+

    Is this it?

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well, your string should start with 0 or more digits, then
    one letter or space, then 0 or more letters, digits or spaces. So

    /^[0-9]*[a-zA-Z ][\w ]*$/

  4. #4
    SitePoint Guru momos's Avatar
    Join Date
    Apr 2004
    Location
    Belgium
    Posts
    920
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog
    well, your string should start with 0 or more digits, then
    one letter or space, then 0 or more letters, digits or spaces. So

    /^[0-9]*[a-zA-Z ][\w ]*$/
    What is wrong with the \w+ expression? It's plain simply 1 or more alphanumeric chars... as far as I understand it...

  5. #5
    SitePoint Guru SteveO's Avatar
    Join Date
    Jan 2003
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    \w+ will allow more than 1 digit.

    Would this work? ([a-z][A-Z])*([0-9])+?([a-z][A-Z])*

  6. #6
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks to me like stereofrog's got it right, although it could be refined a bit

    /^[a-z0-9 ]*[a-z][a-z0-9 ]*$/i

    I prefer this one, because

    1) It won't pass w/just a string that's just a space
    2) Doesn't allow the underscore (\w does)
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  7. #7
    SitePoint Wizard
    Join Date
    Jan 2004
    Location
    3rd rock from the sun
    Posts
    1,005
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.regular-expressions.com/javascript.html had that on my clipboard, may come in handy if you want to start grappling with regexs.

  8. #8
    SitePoint Guru momos's Avatar
    Join Date
    Apr 2004
    Location
    Belgium
    Posts
    920
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm actually still in a learing curve when it comes to RegExp, that is also why I started this tread:

    http://www.sitepoint.com/forums/showthread.php?t=167372

    It always feels like not being able to check everything...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •