Regular expressions

Discussion in 'PHP' started by Alley Cat, Feb 5, 2008.

  1. #1
    In my form pages I use regular expressions for items which need to be typed in by users, below is an example
    
    // Check for an email address.
    if (eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,40}$', stripslashes(trim($_POST['email'])))) {	
    	$e = escape_data($_POST['email']);
    	} else {
    		$e = FALSE;
    		echo '<p>Please enter a valid email address!</p>';
    	}
    
    Code (markup):
    but I would like to know how I can adapt this code for use on items where a user has to choose prepared options, like radio buttons, and items in arrays.
     
    Alley Cat, Feb 5, 2008 IP
  2. Adrians

    Adrians Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You won't need regular exressions in this case because the user can't give other inputs than the prepared ones, so he won't input other things like invalid email adresses, malformed URL-s or malitious code.

    And to "see" from the script what a user has chosen, take this example with radio buttons:

    The form:
    <input type="radio" name="form1" value="ABC"> abc<br>
    <input type="radio" name="form1" value="DEF" checked> def<br>
    <input type="radio" name="form1" value="GHI"> ghi

    The processing:
    echo $_POST['form1']; // will print ABC or DEF or GHI

    If there is no option set by the user, you can verify using isset($_POST['form1']) .
     
    Adrians, Feb 7, 2008 IP
  3. daman371

    daman371 Peon

    Messages:
    121
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I think he's trying to just check the email in the form for some validity. It's a psuedo fool proof method. The only foolproof method is to send a validation email. Try this code.

    
    if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", stripslashes(trim($_POST['email']))))
    {
    $e = escape_data($_POST['email']);
    }
    else
    {
    $e = FALSE;
    echo '<p>Please enter a valid email address!</p>';
    }
    
    PHP:
     
    daman371, Feb 7, 2008 IP
  4. Alley Cat

    Alley Cat Peon

    Messages:
    41
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    So what I would need to do with my $t=$_POST['track']; is
     
    Alley Cat, Feb 7, 2008 IP