markwill2112 Posted January 21, 2011 Report Share 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 Quote Link to comment Share on other sites More sharing options...
markwill2112 Posted January 21, 2011 Author Report Share Posted January 21, 2011 No I lied - still not working. Quote Link to comment Share on other sites More sharing options...
falkencreative Posted January 22, 2011 Report Share 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. Quote Link to comment Share on other sites More sharing options...
markwill2112 Posted January 26, 2011 Author Report Share Posted January 26, 2011 AWESOME! That did the trick. Many thanks! I see now how it can be done from your post. Excellent! Quote Link to comment Share on other sites More sharing options...
BeeDev Posted January 26, 2011 Report Share 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; } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.