Can someone please combine those 2 scripts into one, so I have only 1 onSubmit function in form. <SCRIPT LANGUAGE="JavaScript"> <!-- function Validate(form) { var v = new RegExp(); v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"); if (!v.test(form["link"].value)) { alert("You must supply a valid URL."); return false; } } //--> </SCRIPT> Code (markup): <script language="javascript"> function checkMail(email) { var mail = email.value; var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]*{1}[a-z0-9]+)*[\.]{1}(com|ca|net|org|fr|us|qc.ca|gouv.qc.ca)$', 'i'); if(!reg.test(mail) || mail == "") { alert("Your email address isn't valid!"); return false; } else { } } </script> Code (markup):
<SCRIPT LANGUAGE="JavaScript"> <!-- function Validate(form,email) { var v = new RegExp(); v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"); if (!v.test(form["link"].value)) { alert("You must supply a valid URL."); return false; } var mail = email.value; var reg = new RegExp('^[a-z0-9]+([_|\.|-]{1}[a-z0-9]+)*@[a-z0-9]+([_|\.|-]*{1}[a-z0-9]+)*[\.]{1}(com|ca|net|org|fr|us|qc.ca|gouv.qc.ca)$', 'i'); if(!reg.test(mail) || mail == "") { alert("Your email address isn't valid!"); return false; } else { } } //--> </SCRIPT>
that's ok. return false - i thought you knew that and just wanted the regex help var fields = { email: document.getElementById("email"), homepage: document.getElementById("url") }; document.getElementById("check").onclick = function() { var errors = false, errorFields = []; if (!fields.email.value.isEmail()) { alert(fields.email.value + " is not a valid email."); errors = true; errorFields.push(fields.email); } if (!fields.homepage.value.isURL()) { alert(fields.homepage.value + " is not a valid url."); errors = true; errorFields.push(fields.homepage); } // you can iterate through the errorFields array after and do things like, make the background red etc to show on screen whats wrong. if (errors === true) return false; }; PHP: also, (com|ca|net|org|fr|us|qc.ca|gouv.qc.ca) ??? so a .co.uk fails? in fact, 99% of TLDS fail. nice idea! french canadians script is it?
oh yes. when it's a form submit, then the event needs to change. your markup is: <form action="payment.php" method="get" name="formcheck" onsubmit="return formCheck(this)"> <div class="tekst">*Link:</div><br> <input name="link" type="text" class="TextField" value="http://www." size="35" id="url"> <br><br> <div class="tekst">*Email:</div><br> <input name="email" type="text" class="TextField" size="35" id="email"> <input type="image" src="images/send-button.jpg" name="submit" value="submit" id="check"> </form> PHP: change it to: <form action="payment.php" method="get" name="formcheck" id="formcheck"> <div class="tekst">*Link:</div><br> <input name="link" type="text" class="TextField" value="http://www." size="35" id="url"> <br><br> <div class="tekst">*Email:</div><br> <input name="email" type="text" class="TextField" size="35" id="email"> <input type="image" src="images/send-button.jpg" name="submit" value="submit"> </form> ... <script type="text/javascript"> (function() { // create the string prototypes in a closed scope. var formVerifier = { regex: { email: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/, // " url: /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i }, init: function() { String.prototype.isEmail = function() { // defer to parent by name because 'this' won't be available to prototype return this.match(formVerifier.regex.email); }; String.prototype.isURL = function() { return this.match(formVerifier.regex.url); }; } }; formVerifier.init(); })(); window.onload = function() { var fields = { email: document.getElementById("email"), homepage: document.getElementById("url") }; document.getElementById("formcheck").onsubmit = function() { var errors = false, errorFields = []; if (!fields.email.value.isEmail()) { alert(fields.email.value + " is not a valid email."); errors = true; errorFields.push(fields.email); } if (!fields.homepage.value.isURL()) { alert(fields.homepage.value + " is not a valid url."); errors = true; errorFields.push(fields.homepage); } // you can iterate through the errorFields array after and do things like, make the background red etc to show on screen whats wrong. if (errors === true) return false; }; // end onsubmit event. }; // end onload </script> PHP: notice - assign an id to form, then handle the onsubmit. handling the click was for demonstration purposes. a click on a button may return false but the submit event still fires.