Jump to content

Required Fields not all working on webform


markwill2112

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;  }
}

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...