SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regex for this ??

    Hello forums!!
    I would like to know the regex for the following:
    input text can contain
    alphanumeric characters along with underscore and space
    for example: "this is user_name"
    i used the following:
    PHP Code:
    /^[a-zA-Z0-9 _]+$/ 
    this works but not perfectly because when the only space bar is pressed it takes the value, i want to prevent this, space should be @ the beginning, what will be the regex for this ?
    Thanks in advance to all of you

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    trim the value of whitespace before taking it.

    PHP Code:
    $input trim($input); 
    that will remove spaces/whitespace at the beginning and end of the string.

    if it is just a space the $input will be empty ($input == '')
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    Chessplayer kleineme's Avatar
    Join Date
    Apr 2004
    Location
    Germany
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PHPycho
    space should be @ the beginning
    What does this mean? In your example there is no space at the beginning.
    Never ascribe to malice,
    that which can be explained by incompetence.
    Your code should not look unmaintainable, just be that way.


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
  •