www.webdeveloper.com
Results 1 to 10 of 10

Thread: Form fields that MUST be filled in

  1. #1
    Join Date
    May 2004
    Posts
    288

    Form fields that MUST be filled in

    Ive created a form in PHP which sends an automatic email to me.

    I want the the form to fuction so that the email address field MUST be filled in.

    how is this achieved?

    thanks in advance

  2. #2
    Join Date
    Jan 2006
    Posts
    63
    I use both Javascript on the client side to validate if it's an email address, and PHP on the backend to check if it's valid. You'll basically be able to search for the @ and . with the strings in between.

  3. #3
    Join Date
    May 2004
    Posts
    288
    would you be able to help me out with this?

    this is all new to me

  4. #4
    Join Date
    Jan 2006
    Posts
    63
    I don't have any code off hand, but it's not bad. Just grab the form field by ID in Javascript, put it into a variable, and run a check on what is actually in the variable. If there's 'something'@'something'.'something' return true, else return false. Maybe someone else has some code for you.

  5. #5
    Join Date
    May 2004
    Posts
    288
    Anyone Have This Code Handy?

  6. #6
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,614
    PHP Code:
    // assume form field name is "email":
    $email trim($_POST['email']); // get rid of leading/trailing whitespace
    if(empty($email))
    {
      
    // handle error here (display error message, or return to form via header(), etc.)
    }
    else
    {
      
    // proceed with form processing

    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  7. #7
    Join Date
    May 2006
    Location
    Kirkland, WA
    Posts
    5
    EDIT: Hrm, this is actually way overkill for what you want. If you just want to verify the field is not empty, then NogDog nailed it for you.

    Error checking e-mail is not fun. This is by no means perfect, but its a start (should be used after you've checked to make sure the field is not empty).

    PHP Code:
       // Check syntax
       
    $validEmailExpr =  "^[0-9a-z~!#$%&_-]([.]?[0-9a-z~!#$%&_-])*" .
                          
    "@[0-9a-z~!#$%&_-]([.]?[0-9a-z~!#$%&_-])*$";

       if (!
    eregi($validEmailExpr$_SESSION["{$formVars}"]["{$field}"]))
       {
          
    $_SESSION["{$errors}"]["{$field}"] =  
             
    "The email must be in the name@domain format.";
          return 
    false;
       } 
    I did not write this originally, just a snippet I'd stored at some point. Another option would be to verify the the e-mail domain exists by checking the MX record or just doing a hostname lookup.

  8. #8
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,614
    Here's the email format validation function I use:
    PHP Code:
    # email address validation function
    # kudos to http://iamcal.com/publish/articles/php/parsing_email/pdf/
    function is_valid_email_address($email) {
      
    $qtext '[^\\x0d\\x22\\x5c\\x80-\\xff]';
      
    $dtext '[^\\x0d\\x5b-\\x5d\\x80-\\xff]';
      
    $atom '[^\\x00-\\x20\\x22\\x28\\x29\\x2c\\x2e\\x3a-\\x3c'.
      
    '\\x3e\\x40\\x5b-\\x5d\\x7f-\\xff]+';
      
    $quoted_pair '\\x5c\\x00-\\x7f';
      
    $domain_literal "\\x5b($dtext|$quoted_pair)*\\x5d";
      
    $quoted_string "\\x22($qtext|$quoted_pair)*\\x22";
      
    $domain_ref $atom;
      
    $sub_domain "($domain_ref|$domain_literal)";
      
    $word "($atom|$quoted_string)";
      
    $domain "$sub_domain(\\x2e$sub_domain)*";
      
    $local_part "$word(\\x2e$word)*";
      
    $addr_spec "$local_part\\x40$domain";
      return 
    preg_match("!^$addr_spec$!"$email) ? 0;

    This is a very complete interpretation of the RFC822 email address specification, and should give no false positives or false negatives (as far as format goes, not whether the address actually exists).
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  9. #9
    Join Date
    May 2006
    Location
    Kirkland, WA
    Posts
    5
    Ah, very nice NogDog.

    /me copies for later use

    Thanks!

  10. #10
    Join Date
    Apr 2003
    Location
    Rosemount, MN
    Posts
    2,287
    If you still need help with empty feilds, than Here is a code I made: http://php.d-top.org/index.php?page=empty_feilds

    at the very bottom, their is this line: mysql_query("INSERT INTO id(`first`, `last`) VALUES ('$first', '$last')") or die(mysql_error());

    With that line, you would switch that out with you email code, as posted above.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles