Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Dec 2005
    Posts
    217
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Give this a shot:
    PHP Code:
    if ( preg_match'/[\w\s]+/'trim$var ) ) )
        echo 
    'good';
    else
        echo 
    'bad'
    Last edited by kbluhm; 06-06-2007 at 05:00 AM.

  • #3
    Regular Coder
    Join Date
    Apr 2007
    Posts
    295
    Thanks
    0
    Thanked 19 Times in 19 Posts
    If you want to prevent only spaces from being entered, run it through trim() first to remove all spaces before and after the string


  •  

    Posting Permissions

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