SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb validation hybrid

    Hi all,

    I am trying to join to pieces of code

    the first is very easy

    if ($_POST["Email"] =="" || $_POST["Email"] =="your@email.com"){

    header("Location: index.html");
    exit;
    }

    how can i combine the function of the above code with something like this below

    function is_valid_email($email) {
    $result = TRUE;
    if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
    $result = FALSE;
    }
    return $result;
    }

    It dosen't need to echo a message just simply reload the html form that called the php

    Any help would be massivly appreciated

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Read: ereg deprecated

    PHP Code:
    function is_valid_email($email) {
    $result FALSE;

    // # is the delimiter, the i after last # is a switch denoting upper/lower case

    if(preg_match("#^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$#i"$email)) {
    $result TRUE;
    }

    return 
    $result;

    IF your regex is ok (none of this is tested btw) you'd use it like this:

    PHP Code:
    if ($_POST["Email"] =="" || is_valid_email($_POST["Email"] ) ) {
    header("Location: index.html");
    exit;    


  3. #3
    SitePoint Zealot
    Join Date
    Mar 2011
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks heaps for your reply

    im kinda a noob ..regex??

    When the field is empty it still connects the form to the location html.

    and when its not valid(eg asdf.com) the page just goes blank.

    Also when your@email.com is submitted it still refers the loaction html

    Though when a valid format is submitted it links to the location html (like i would like)

    I know its a long shot but would you help me further, please?????

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Well I don't know what the rest of your page is doing and I dont know what your form contains exactly.

    Temporarily tell your form to submit to this test page (ie set <form action="testpage.php" method=""POST">)

    testpage.php
    PHP Code:
    <?php

    var_dump
    ($_POST['Email']);

    echo 
    '<hr />';

    function 
    is_valid_email($email) {
      
    $result FALSE;
      if(
    preg_match("#^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$#i"$email)) {
      
    $result TRUE;
      }
    return 
    $result;
    }  

    if (
    $_POST["Email"] =="" || is_valid_email($_POST["Email"] ) ) {

    echo 
    'Sending you to Location: index.html';
        
    } else {

    echo 
    'Showing you the form.... ';

    }
    Show us the output of that page when Email is not filled in, when its filled in with a non-email "asdfasdf" and when its got a valid email "asdf@asdf.com".

    Sorry but I'm not in a position to test any of this, just guessing at the code, which is not ideal ...

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Slight modification...
    Code:
    if ($_POST["Email"] =="" || !(is_valid_email($_POST['Email']) ) ) {
    echo 'Showing you the form.... ';
    } else {
    	echo 'Sending you to Location: index.html';
    }


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
  •