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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Dec 2008
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php validation(no spaces and special chr's)

    please i want to know how to make a validation using php in the way that only characters(A-Z, a-z) , number and _ are allowed to be used

    special characters and spaces are not allowed (will return error message)


    can anyone help me please

  • #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
    PHP Code:
    if ( preg_match'/\W/'$input ) )
    {
        
    // illegal characters exist
    }

    // or...
    if ( preg_match'/^\w+$/'$input ) )
    {
        
    // all characters are legal


  • #3
    Regular Coder
    Join Date
    Dec 2008
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by kbluhm View Post
    PHP Code:
    if ( preg_match'/\W/'$input ) )
    {
        
    // illegal characters exist
    }

    // or...
    if ( preg_match'/^\w+$/'$input ) )
    {
        
    // all characters are legal

    Sorry i cant understand what did you mean.. could you please explain more?

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Yeah, still early in the day here.

    \w matches any word character, which includes (tada!) letters, numbers, and underscores only.

    \W does just the opposite, and matches anything that is not a word character.

    \w+ matches 1 or more consecutive word characters, ^ signifies the start of the subject or line, and $ signifies the end.

    So ^\w+$ will only match a subject that is made up of 1 or more word characters.
    Last edited by kbluhm; 01-21-2010 at 02:51 PM.


  •  

    Posting Permissions

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