Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Optional Email address validation

    Hi

    I can validate an email address but can't make it optional if the field isn't filled in - has anyone any ideas on what to add to my code?

    PHP Code:
    <html>
    <
    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('Please enter a value for the "' fieldLabel +'" field.');
            
    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 enter a complete email address in the form: [email]yourname@yourdomain.com[/email]");
            
    formField.focus();
            
    result false;
        }
       
      return 
    result;

    }

    function 
    validateForm(update)
    {

        
        if (!
    validEmail(update.email,"Email Address"false))
            return 
    false;        

        return 
    true;

    }

    </script>
    </head>
    <body>
    <form name="update" action="test.php" method="post" onsubmit="return validateForm(this)">
    <table>
      <tr>
        <td width="40%"><b>Email address:</b></td>
        <td width="60%"><input type="text" name="email" maxlength="100" size="20"></td>
      </tr>
      <tr>
        <td width="100%" colspan="2"><center><input type="submit" name="Update" value="Update"></center></td>
      </tr>
    </table>
    </form>
    </body>
    </html> 
    Thanks

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This script I have handy may give you some ideas.
    Code:
    <script type="text/javascript">
     <!--//
      var agree = "";
      function validateForm(form){
        for(var i=0; i<form.elements.length; i++){
          if(form.elements[i].value=="" && form.elements[i].name!="email"){
           var msg = 'Please enter a value in '+form.elements[i].name.toUpperCase();
             alert(msg);
             form.elements[i].focus();
             return false;
          }
        }
    
        if(agree==""){
         var msg = 'Would you like to enter your email address?';
          agree = confirm(msg);
          if(agree){ 
            form.email.focus();
            return false;
          }
          else{
            form.email.value="";
            return true;
          }
        }
    
        var goodEmail = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/.test(form.email.value);
        if(agree && form.email.value=="" || !goodEmail){
         var msg = 'Are you sure you wish to enter your email address?';
             msg+= '\nIf so, please enter a valid email address.';
             agree = confirm(msg);
          if(agree){
             form.email.value = ''; 
             form.email.focus();
             return false;
          }
          else{
             form.email.value="";
             return true;
          }
        }
        return true;
      }
     //-->
    </script>
    </head>
    
    <body>
    <form method="post" action="mailto:" onsubmit="return validateForm(this)">
    NAME:  <input type="text" name="name"><br>
    EMAIL: <input type="text" name="email"><br>
    ADDRESS: <input type="text" name="address"><br>
    <input type="submit" value="Send">
    <input type="reset" value="Reset">
    </form>

    .....Willy

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    UK
    Posts
    262
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cheers willy

    do u know how to do if so that if they don't fill in an email address it passes through and if they do fill it in it will validate?

    as you can see i have the code to validate but it seems to validate even if no email address is inserted!

    Thanks

  • #4
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That is what the script I posted does. If you do not want to confirm if the user wants to fill in the email addrress. Remove the if(agree) conditional statement and any reference to it.

    Also the below does much more validating in one line than the 20 or so lines you are using to validate @ and/or length is more than three. I would recommend that you use this regular expression instead of the routine you are using.
    /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/.test(form.email.value);

    FWIW: If you would like your script to validate an empty text box. Remove this conditional statement and any reference to it.
    if (formField.value == "")


    .....Willy


  •  

    Posting Permissions

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