SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help on my function(); it is very small, but I am not quite sure how to do it.

    Hi,

    I have made a function and have put it into a for() loop so that it can change one function to make 12 of them with different names.

    How can I write the $ie in the function properly so that it doesn't throw up errors.

    PHP Code:
    for($ie=0$ie 13$ie++){

    function 
    valfmail$ie($fmail$ie){
          
    $patf "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
      if (
    eregi($patf$fmail$ie)){
         return 
    true;
    }else{
         return 
    false;
    }}

    Thanks!

  2. #2
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, your function should sit outside of your procedural code something like this:
    PHP Code:
    for($ie=0$ie 13$ie++){
        
    $result val($ie);


    function 
    val($ie){
        
    $patf "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
          if (
    eregi($patf$ie)){
             return 
    true;
        }
        else
        {
            return 
    false;
        }


  3. #3
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, but I can't seem to get it to work.

  4. #4
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what behavior would be considered "working"?

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have 12 fields from a form that all have similar names.

    They are named like the following:

    field1
    field2
    field3
    etc.

    No I want to have a function so that I can validate the email address for them 12 fields. The function has to be the same, but it has to work with all them email fields to validate each one.

    So I thought of putting a function into a for loop so that I can change the functions name and things so that it is the same as the fields, but I guess that will not work.

    Hope you can help!

    Thanks!

  6. #6
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    219
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so you want something similar to this maybe:

    This supposes that you have an array of form fields called email (note the square brackets):

    HTML Code:
    <input type="text" name="email[]" />
    You could then do something like:


    PHP Code:

    if(isset($_POST['email']))
    {
      for(
    $ie=0$ie sizeof($_POST['email']); $ie++){
        
    $result val($_POST['email'][$ie]);
        
    //do something here based on $result..
    }

    function 
    val($email){
        
    $patf "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
          if (
    eregi($patf$email)){
             return 
    true;
        }
        else
        {
            return 
    false;
        }
    }


  7. #7
    SitePoint Wizard
    Join Date
    Nov 2003
    Location
    United Kingdom
    Posts
    2,120
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.


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
  •