emmaclarke Posted June 20, 2009 Share Posted June 20, 2009 I'm trying to get that the "first part" of the postcode is checked against whether it is MK17 for example. I think my if statement pattern checks if MK17 is in any part of the postcode so i'm not sure how to check it for the first part of the post code.Also the alert box won't go away. It keeps popping up even though it is in an if statement. function validateOnSubmit() { // When the form is submitted, we revalidate all the fields in the // form and then check their classNames to see if they are invalid. // If any of those fields are invalid, display an alert and prevent // form submission. var invalid = false; // Start by assuming everything is valid // Loop through all form elements for(var i = 0; i < this.elements.length; i++) { var e = this.elements[i]; // If the element is a text field and has our onchange handler if (e.type == "text" && e.onchange == validateOnChange) { e.onchange(); // Invoke the handler to re-validate // If validation fails for the element, it fails for the form if (e.className == "invalid") invalid = true; } if(pattern = "MK1" || "MK2" || "MK3" || "MK4" || "MK5" || "MK6" || "MK7" || "MK8" || "MK9" || "MK10" || "MK11" || "MK12" || "MK13" || "MK14" || "MK15" || "MK17" || "MK19" || "MK77") { alert("You will receive a special gift"); } } Link to comment Share on other sites More sharing options...
Ingolme Posted June 20, 2009 Share Posted June 20, 2009 I don't see where pattern is defined "MK1" || "MK2" || "MK3" ... will always return true pattern = "MK1" || ... ...: You are assigning the result of all those || operations to the pattern variable, you need to use the == operator to compare things. Supposing that pattern is defined somewhere in the script, you would probably want to do this: if( pattern == "MK1" || pattern == "MK2" || pattern == "MK3" || pattern == "MK4" || pattern == "MK5" || pattern == "MK6" || pattern == "MK7" || pattern == "MK8" || pattern == "MK9" || pattern == "MK10" || pattern == "MK11" || pattern == "MK12" || pattern == "MK13" || pattern == "MK14" || pattern == "MK15" || pattern == "MK17" || pattern == "MK19" || pattern == "MK77") But I doubt that's going to work either because I have no idea what pattern is and where it's defined. Link to comment Share on other sites More sharing options...
emmaclarke Posted June 21, 2009 Author Share Posted June 21, 2009 (function() { // Do everything in this one anonymous function // When the document finishes loading, call init() if (window.addEventListener) window.addEventListener("load", init, false); else if (window.attachEvent) window.attachEvent("onload", init); // Define event handlers for any forms and form elements that need them. function init() { // Loop through all forms in the document for(var i = 0; i < document.forms.length; i++) { var f = document.forms[i]; // the form we're working on now // Assume, for now, that this form does not need any validation var needsValidation = false; // Now loop through the elements in our form for(j = 0; j < f.elements.length; j++) { var e = f.elements[j]; // the element we're working on // We're only interested in <input type="text"> textfields if (e.type != "text") continue; // See if it has attributes that require validation var pattern = e.getAttribute("pattern"); // We could use e.hasAttribute() but IE doesn't support it var required = e.getAttribute("required") != null; // Required is just a shortcut for a simple pattern if (required && !pattern) { pattern = "\\S"; e.setAttribute("pattern", pattern); } // If this element requires validation, if (pattern) { // validate the element each time it changes e.onchange = validateOnChange; // Remember to add an onsubmit handler to this form needsValidation = true; } } // If at least one of the form elements needed validation, // we also need an onsubmit event handler for the form if (needsValidation) f.onsubmit = validateOnSubmit; } } // This function is the onchange event handler for textfields that // require validation. Remember that we converted the required attribute // to a pattern attribute in init(). function validateOnChange() { var textfield = this; // the textfield var pattern = textfield.getAttribute("pattern"); // the pattern var value = this.value; // the user's input // If the value does not match the pattern set the class to "invalid". if (value.search(pattern) == -1) textfield.className = "invalid"; else textfield.className = "valid"; } // This function is the onsubmit event handler for any form that // requires validation. function validateOnSubmit() { // When the form is submitted, we revalidate all the fields in the // form and then check their classNames to see if they are invalid. // If any of those fields are invalid, display an alert and prevent // form submission. var invalid = false; // Start by assuming everything is valid // Loop through all form elements for(var i = 0; i < this.elements.length; i++) { var e = this.elements[i]; // If the element is a text field and has our onchange handler if (e.type == "text" && e.onchange == validateOnChange) { e.onchange(); // Invoke the handler to re-validate // If validation fails for the element, it fails for the form if (e.className == "invalid") invalid = true; } if(pattern = "MK1" || "MK2" || "MK3" || "MK4" || "MK5" || "MK6" || "MK7" || "MK8" || "MK9" || "MK10" || "MK11" || "MK12" || "MK13" || "MK14" || "MK15" || "MK17" || "MK19" || "MK77") { alert("You will receive a special gift"); } } // If the form is invalid, alert user and block submission if (invalid) { alert("The form is incompletely or incorrectly filled out.\n" + "Please correct the highlighted fields and try again."); return false; } }})(); Link to comment Share on other sites More sharing options...
emmaclarke Posted June 21, 2009 Author Share Posted June 21, 2009 if( pattern == "MK1" || pattern == "MK2" || pattern == "MK3" || pattern == "MK4" || pattern == "MK5" || pattern == "MK6" || pattern == "MK7" || pattern == "MK8" || pattern == "MK9" || pattern == "MK10" || pattern == "MK11" || pattern == "MK12" || pattern == "MK13" || pattern == "MK14" || pattern == "MK15" || pattern == "MK17" || pattern == "MK19" || pattern == "MK77") This doesn't show an alert box when the post code begins with any of the MK19 numbers. Link to comment Share on other sites More sharing options...
emmaclarke Posted June 21, 2009 Author Share Posted June 21, 2009 <td>Postal Code</td> <td><input type="text" name="shipPostalCode" maxlength= "30" pattern="\w{2,}\d{1,2}\s*\d{1,2}\w{2,}" /></td> </tr> this is the form data in the html web page, this pattern just checks if its like 1 or 2 letters followed by 1 or 2 numbers etc.pattern is in the validate.js that i posted in the last post.somewhere validation needs to take place on the type of postcode and to s how a message saying that they have received a gift if from MK1-15 etc. i put this in the validate.js on submit form as u can see.it does nothing now tha ti have amended it to your == Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.