SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    simple: At least one email required - validation?

    Hi there,

    I am creating a tell-a-friend where a user can put a max of 3 friend's email, but at least one. So i want to use javascript to check if at least 1 email is being filled in.

    Javascript.
    PHP Code:
    function validRequired(formField,fieldLabel)
    {
      var 
    result true;
      if (
    formField.value == "")
      {
        
    alert('Enter a value in "' fieldLabel +'"');
        
    formField.focus();
        
    result false;
      }
      return 
    result;
    }
     
    function 
    validEmail(formField,fieldLabel,required)
    {
      var 
    result true;
      if (
    required && !validRequired(formField,fieldLabel))
        
    result false;
      if (
    result && ((formField.value.length 3) || !isEmailAddr(formField.value)) )
      {
        
    alert("Please, use a valid email for Friend's Email");
        
    formField.focus();
        
    result false;
      }
      return 
    result;

    NOw i tried a couple of things but the most recent one is:
    PHP Code:
    function validateForm(theForm)
    {
      
    // Customize these calls for your form
      // Start ------->
      
    if (!validRequired(theForm.email1,"Friend's Email"))
        return 
    false;
      if (!
    validEmail(theForm.email1,"Friend no 1 Email",true))
        return 
    false;
      if (!
    validEmail(theForm.email2,"Friend no 2 Email",false))
        return 
    false;
      if (!
    validEmail(theForm.email3,"Friend no 3 Email",false))
        return 
    false;
     
      
    // <--------- End
      
    return true;

    But this doesn't do the job. Why not and what is the solution???

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Maryland, USA
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The logic in the code seems right. What is the error?

    The only thing I see is that the following line is redundant:

    Code:
    if (!validRequired(theForm.email1,"Friend's Email" ))
        return false;
    Since this is checked when in validEmail().

  3. #3
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    somehow the boolean statement isn't evaluated. any more suggestions

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2003
    Location
    Maryland, USA
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tested out your code, and the following statement does return back a true or false.

    Code:
    alert(!validRequired(theForm.email1,"Friend's Email" ));
    I am not sure what is going on here, since the code seems error free. Have you tried using try/catch?

  5. #5
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    1) which boolean isn't executing?
    2) what does your form look like
    3) checking for an empty string does not prove validity
    4) you might be interested in the "Validating Email" script/tutorial at my site (GrassBlade)

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  6. #6
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hi,

    My code
    PHP Code:
    <html>
    <
    head>
    <
    title>//Tell-a-friend</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </
    head>
    <
    script Language="JavaScript">
    function 
    isEmailAddr(email)
    {
    var 
    result false;
    var 
    theStr = new String(email);
    var 
    index theStr.indexOf("@" );
    if (
    index 0)
    {
    var 
    pindex theStr.indexOf(".",index);
    if ((
    pindex index+1) && (theStr.length pindex+1))
    result true;
    }
    return 
    result;
    }
    function 
    validRequired(formField,fieldLabel)
    {
    var 
    result true;
    if (
    formField.value == "" )
    {
    alert('Enter a value in "' fieldLabel +'"');
    formField.focus();
    result false;
    }
    return 
    result;
    }
    function 
    validEmail(formField,fieldLabel,required)
    {
    var 
    result true;
    if (
    required && !validRequired(formField,fieldLabel))
    result false;
    if (
    result && ((formField.value.length 3) || !isEmailAddr(formField.value)) )
    {
    alert("Please, use a valid email for Friend's Email" );
    formField.focus();
    result false;
    }
    return 
    result;
    }
    function 
    validateForm(theForm)
    {
    // Customize these calls for your form
    // Start ------->
    if (!validRequired(theForm.name1,"Friend's Name" ))
    return 
    false;
    if (!
    validEmail(theForm.email1,"email1",true))
    return 
    false;
    if (!
    validEmail(theForm.email2,"email2",false))
    return 
    false;
    if (!
    validEmail(theForm.email3,"Friend no 3 Email",false))
    return 
    false;
    if (!
    validEmail(theForm.sender_email,"Your Email",true))
    return 
    false;
    // <--------- End
    return true;
    }
    </script>
    <body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
    <table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
    <tr>
    <td><div align="center">
    <table width="450" border="0" cellspacing="0" cellpadding="0">
    <form name="theForm" action="tell-a-friend2.php" method="post" onsubmit="return validateForm(this)"> <tr>
    <td><TABLE id="Table2" cellSpacing="5" cellPadding="0" width="448" border="0">
    <TR>
    <TD width="25%" height="20">
    <DIV class="NormalTextBold"></DIV>
    </TD>
    <TD width="25%" height="20">
    <DIV >Name of friend</DIV>
    </TD>
    <TD width="25%" height="20">
    <DIV >Friend's Email </DIV>
    </TD>
    <TD width="25%" height="20"></TD>
    </TR>
    <TR>
    <TD width="25%" height="28">
    <DIV>1.</DIV>
    </TD>
    <TD>
    <input name="name1" type="text" id="name3" class="normalText" />
    </TD>
    <TD>
    <input name="email1" type="text" size="25" id="email1" class="normalText" />
    </TD>
    </TR>
    <TR>
    <TD width="25%" height="27">
    <DIV>2.</DIV>
    </TD>
    <TD>
    <input name="name2" type="text" id="name2" class="normalText" />
    </TD>
    <TD>
    <input name="email2" type="text" size="25" id="email2" class="normalText" />
    </TD>
    </TR>
    <TR>
    <TD width="25%" height="22">
    <DIV>3.</DIV>
    </TD>
    <TD>
    <input name="name3" type="text" id="name3" class="normalText" />
    </TD>
    <TD>
    <input name="email3" type="text" size="25" id="email3" class="normalText" />
    </TD>
    </TR>
    </TABLE>
    </td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td><TABLE id="Table3" cellSpacing="5" cellPadding="0" width="450" border="0">
    <TR>
    <TD width="142" height="22">
    <DIV >Your email address</DIV>
    </TD>
    <TD width="293">
    <input name="sender_email" type="text" size="30" id="senderName" class="normalText" />
    </TD>
    </TR>
    </TABLE>
    </td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    </tr>
    <tr>
    <td><input type="submit" name="Submit" value="Submit">
    </td>
    </tr>
    </form>
    </table>
    </div></td>
    </tr>
    </table>
    </body>
    </html> 
    Now what i want to accomplish:
    At least 1 email is required and email2 and email3 ar optional

    I thougth by setting "true"or "false" in
    PHP Code:
    if (!validEmail(theForm.email1,"email1",true"<----here)) 
    does the job. But it ain't you can copy & paste and test it yourself.

    Any other suggestions

    ps sorry for the long post
    Last edited by D-flyer; Apr 12, 2005 at 12:40.

  7. #7
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    a few quick comments:

    1) kill the requireds
    2) check names first -- if there is no name, you don't want an email address
    2) embed the if statements in validateForm; you don't want concurrent validation; you want conditional validation -->
    check the 2nd name/addr pair only if the 1st address is ok -- if it is bad, discard them

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  8. #8
    SitePoint Guru D-flyer's Avatar
    Join Date
    Jan 2001
    Location
    Near a computer
    Posts
    782
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi vinny,

    I am trying to use your suggestions, but i don't seem to get it:
    PHP Code:
    function validateForm(theForm)
    {
    // Customize these calls for your form
    // Start ------->
    if (!validRequired(theForm.name1,"Friend's Name"))
        return 
    false;
    if (!
    validEmail(theForm.email1,"email1"))
        return 
    false;
    theForm.name2== "" ? (validEmail(theForm.email2"email2")):(!validEmail(theForm.email2"email2"));
    theForm.name3== "" ? (validEmail(theForm.email3"email3")):(!validEmail(theForm.email3"email3")); 
     
    if (!
    validRequired(theForm.sender_email,"Your email"))
        return 
    false;
    if (!
    validEmail(theForm.sender_email,"Your Email",true))
        return 
    false;
    // <--------- End
    return true;

    Trying to create a conditional with
    condition ? result_if_true : result_if_false;
    Last edited by D-flyer; Apr 12, 2005 at 12:40.


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
  •