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 to validate nameCode: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 that calls the function and displays errorsCode:function submitValidName(name) { if (name.length > 0) { return true; } else { return false; } }
HTML for the formCode:<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 += " "; 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 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"> </td> </tr> <tr id="errorrow" style="display: none;"> <td colspan="2" id="errors" class="error"> </td> </tr> <tr> <td colspan="2"><p align="center"><input type="submit" value="Submit" onclick="return validateForm()" class="submit" /></p></td> </tr> </table> </form>


Reply With Quote
Bookmarks