www.webdeveloper.com
Results 1 to 2 of 2

Thread: [RESOLVED] Form Validation Error

  1. #1
    Join Date
    Oct 2009
    Posts
    34

    resolved [RESOLVED] Form Validation Error

    I'm having issues with some javascript form Validation. What makes it weird is that the code works perfectly fine on another page (the only difference is some of the inputs and the number of inputs being checked. I have all of my validation code in a separate *.js file.

    Code to validate email
    Code:
    function submitValidEmail(email)
    {
      var valid = /^([0-9]|[A-Z]|[a-z]|-|_|\.|@)+$/;
      if (valid.test(date))
      {
        if (email == "")
        {
          return false;
        }
        at = email.indexOf("@");
        dot = email.lastIndexOf(".");
        
        if (at < 1 || dot < at + 2 || email.length <= dot + 1)
        {
          return false; 
        }
        else
        {
          return true;
        }
      }
      else
      {
        return false;
      }
    	
    }
    Code to validate name
    Code:
    function submitValidName(name)
    {
      if (name.length > 0)
      {
        return true;
      }
      else
      {
        return false;
      }
    }
    Code that calls the function and displays errors
    Code:
    <script src="validate.js" type="text/javascript">
    function validateForm()
    {
      var errString = "";
      var error = false;
      
      if (!submitValidName(document.getElementById("name").value))
      {
        errString += "- You did not enter your name. Please enter one.<br />";
        error = true;
      }
    	if (!submitValidEmail(document.getElementById("email").value))
    	{
    		errString += "- Please insert a valid email. Ex: person@website.com<br />";
        error = true;
    	}
    	if (error == true)
    	{
    		errString += "&nbsp;";
    		document.getElementById("errors").innerHTML = errString;
    		document.getElementById("errorrow").style.display = "table-row";
    		return false;
    	}
    	else
    	{
    		document.getElementById("errorrow").style.display = "none";
    		return true;
    	}
    }
    </script>
    HTML for the form
    HTML Code:
        <form action="email-subscribe.php" method="post">
          <table width="400" border="0" cellpadding="0" cellspacing="0" id="Profile">
            <tr>
              <td width="110" class="header"><p>Name:</p></td>
              <td width="290"><p><input type="text" id="name" name="name" /></p></td>
            </tr>
            <tr>
            	<td class="header"><p>Email:</p></td>
            	<td><p><input type="text" id="email" name="email" /></p></td>
    				</tr>
            <tr>
            	<td colspan="2">&nbsp;</td>
    				</tr>
            <tr id="errorrow" style="display: none;">
              <td colspan="2" id="errors" class="error">&nbsp;</td>
    				</tr>
    				<tr>
    					<td colspan="2"><p align="center"><input type="submit" value="Submit" onclick="return validateForm()" class="submit" /></p></td>
    				</tr>
          </table>
        </form>

  2. #2
    Join Date
    Oct 2009
    Posts
    34
    Ended up figuring the error by myself. The email validation code was testing an undefined variable "date" rather than the defined variable "email". I guess that's what I get for using chrome, and having this page display to where the error message doesn't show up in the bottom corner of IE.

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