SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Guru
    Join Date
    Feb 2002
    Location
    NZ
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_match again !!

    I'm trying to build a function that looks for:

    1. One character in field.
    2. A maximum of 20 characters
    3. Illegal characters.

    Now the problem I have is that the 2 preg_match's that I'm using dont allow illegal characters. So the message returned isn't correct. I want to be able to return a message using 1 of the above, I dont want a message saying "please fill in your First Name" if in fact illegal characters have been used.

    Heres what I have compliments of forum users which were correct based on the questions I asked at the time.

    PHP Code:

            
    /* make sure field is filled in */ 
            
    if(!preg_match("/\\w{1}/i"$name))
            {
                    
    $feedback .= "Please fill in your First name";
            
            }

            
    /* make sure field is not over filled */
            
    elseif(!preg_match("/^\\w{1,20}$/"$name))
            {
                    
    $feedback .= "Maximum of 20 characters for your First name";
                   
            } 
    Thanks for any help.

  2. #2
    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)
    Use this for your first match
    Code:
            if( preg_match( "/^\S*$/", $name ) )
            {
                    $feedback .= "Please fill in your First name";
            
            }
    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




  3. #3
    SitePoint Guru
    Join Date
    Feb 2002
    Location
    NZ
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You were close.

    Heres the answer I came up with and I'm not saying its perfect by any means.

    Code:
            /* make sure field is filled in */ 
            if(!preg_match("/\S{1}/i", $name))
            {
                    $feedback .= "Please fill in your First name";
                   
                   
            
            }
    
    
            /* make sure field is not over filled */
            elseif(!preg_match("/^\S{1,20}$/", $name))
            {
                    $feedback .= "Maximum of 20 characters for your First name";
                 
            }
    
    
     /* Contains all legal characters */
            elseif(preg_match("/\W{1}/i", $name))
            {
                    $feedback .= "Please remove the illegal characters from your First name";
                    
    	}
    Last edited by Motivated; Jan 20, 2003 at 12:17.

  4. #4
    SitePoint Guru
    Join Date
    Feb 2002
    Location
    NZ
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    beetle any chance you can tell me where to put the returns in this function ???
    Last edited by Motivated; Jan 20, 2003 at 13:27.

  5. #5
    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)
    Sure, if I follow correctly...
    PHP Code:
    function whatever$name )
    {
        
    /* make sure field is filled in */ 
        
    if ( preg_match"/^\\s*$/i"$name ) )
        {
            
    $feedback .= "Please fill in your First name";
            return 
    false;
        }


        
    /* make sure field is not over filled */
        
    elseif ( strlen$name ) > 20 )
        {
            
    $feedback .= "Maximum of 20 characters for your First name";
            return 
    false;
        }

        
    /* Contains all legal characters */
        
    elseif ( preg_match"/\\W/i"$name ) )
        {
            
    $feedback .= "Please remove the illegal characters from your First name";
            return 
    false;
        }
        return 
    true;

    Note that in a regex \W{1} is identical to \W. In otherwords, in absence of a quantifier, a quantity of 1 is assumed for the character/group/atom
    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




  6. #6
    SitePoint Guru
    Join Date
    Feb 2002
    Location
    NZ
    Posts
    620
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thankyou


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
  •