Help with regular expressions and preg_match() in PHP

Discussion in 'PHP' started by learnwebsitedesigncom, Jun 29, 2010.

  1. #1
    Hi everyone, I need help with regular expressions in PHP using the preg_match() function. I know how to allow only certain letters, numbers and/or characters. I need to be able to only allow only a set number of letters or numbers in an input field (for example, 3 or 4).

    here is the code that I have so far:

    
    // if the email field is empty, kill the function and display a message
    if(!$email){
     die('Enter a name');
    }
    // allows only lower and uppercase letters and numbers
    elseif(preg_match('/[^a-zA-Z0-9]/', $email)){
     die('Enter a valid email');
    }
    // outputs the email entered
    else{
     echo "Email: $email";
    }
    
    PHP:
    Thank You
     
    Last edited: Jun 29, 2010
    learnwebsitedesigncom, Jun 29, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    // if the email field is empty, kill the function and display a message
    if(!$email){
     die('Enter a name');
    }
    // allows only lower and uppercase letters and numbers
    elseif(!preg_match('/^[a-z0-9]{3,4}$/i', $email_a)){
     die('Enter a valid email');
    }
    // outputs the email entered
    else{
     echo "Email: $email";
    }
    PHP:
     
    danx10, Jun 29, 2010 IP
  3. learnwebsitedesigncom

    learnwebsitedesigncom Active Member

    Messages:
    264
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    58
    #3
    The code that you gave me worked and is self explanatory.

    Thank You
     
    learnwebsitedesigncom, Jun 29, 2010 IP