markwill2112 0 Report post Posted January 21, 2011 Hello All, Could you possibly take a look at the attached code for me? I have made a number of fields 'required fields' so that if they are not filled in, a message pops up on the webform saying they have to be completed...until all required fields are filled, after which the form can be submitted. I have included 'First Name' and 'Surname' in the 'value' list of required fields. However, if I leave them blank on the form and fill in everything else, the form can still be submitted and there is no prompt saying that those two are required, even though there should be. What am I doing wrong? Why are 'First name' and 'Surname not included as required fields? Thanks Mark Required_fields_java.html Share this post Link to post Share on other sites
markwill2112 0 Report post Posted January 21, 2011 No I lied - still not working. Share this post Link to post Share on other sites
falkencreative 48 Report post Posted January 22, 2011 I think the issue is that they technically aren't empty. By default, they contain text "First name..." and "Surname..." You can fix this by updating the MM_validateForm() function: function MM_validateForm(requiredfields) { field = requiredfields.split(","); alertmsg = ""; for(i=0; i<field.length; i++) { if(document.getElementById(field[i]) && (document.getElementById(field[i]).value=="" || document.getElementById(field[i]).value=="First name..." || document.getElementById(field[i]).value=="Surname...")) alertmsg += ", " + field[i].replace("_", ""); } if (alertmsg.length == 0) return true; else { alert("Please provide " + alertmsg.substr(1)); return false; } } though that probably isn't the most efficient solution. I'm not sure if I have a better option for you though -- maybe someone else will have a comment. Share this post Link to post Share on other sites
markwill2112 0 Report post Posted January 26, 2011 AWESOME! That did the trick. Many thanks! I see now how it can be done from your post. Excellent! Share this post Link to post Share on other sites
BeeDev 2 Report post Posted January 26, 2011 Just a further suggestion: there's also a property called "defaultValue" on form elements which stores the value the form field had when it first loaded. Even if you change the form field value, the defaultValue stays at the same value it was when the page loaded. So instead of comparing the values to a strings like "First name...", "Surname..." (and obviously the number of these comparisons will grow as you add more and more fields...) you can just compare it to the "defaultValue". function MM_validateForm(requiredfields) { field = requiredfields.split(","); alertmsg = ""; for(i=0; i<field.length; i++) { if(document.getElementById(field[i]) && (document.getElementById(field[i]).value=="" || document.getElementById(field[i]).value==document.getElementById(field[i]).defaultValue)) alertmsg += ", " + field[i].replace("_", ""); } if (alertmsg.length == 0) return true; else { alert("Please provide " + alertmsg.substr(1)); return false; } } Share this post Link to post Share on other sites