Jump to content
Killersites Community
markwill2112

Required Fields not all working on webform

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

Share this post


Link to post
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.

Share this post


Link to post
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;  }
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×