how to javascript validateForm

Hello, i have a small form with with about 3 options, i have done the rest of the coding to validate the other fields but am not sure how to validate the other option forms..

here is the form
          <form action="signUp-form.php" method="post" onSubmit="MM_validateForm('Name','','R','Company','','R','Email','','RisEmail');return document.MM_returnValue">
            <table width="315">
              <tr>
                <td width="66" height="30" ><p>Name*: </p></td>
                <td width="666" height="30"><input name="Name" type="text" id="Name" size="25" /></td>
              </tr>
              <tr>
                <td height="30"><p>Company*: </p></td>
                <td height="30"><input name="Company" type="Text" id="Company" size="25" /></td>
              </tr>
              <tr>
                <td height="30"><p>Email*: </p></td>
                <td height="30"><input name="Email" type="Text" id="Email" size="25" /></td>
              </tr>
              <tr>
                <td height="30"><p>Platform*: </p></td>
                <td height="30"><label>
                    <select name="Platform" id="Platform">
                      <option selected>Select a Platform</option>
                      <option>PC</option>
                      <option>Mac</option>
                      <option>Server</option>
                    </select>
                  </label></td>
              </tr>
              <tr>
                <td height="30">&nbsp;</td>
                <td height="30"><input type="submit">
                  </input></td>
              </tr>
            </table>
          </form>

Open in new window



want i want to do is make sure that one of the option is selected...

<option selected>Select a Platform</option>
                      <option>PC</option>
                      <option>Mac</option>
                      <option>Server</option>

Open in new window


here is the function code

<!--form Validator-->
<script type="text/javascript">
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }
</script>

Open in new window

businessesatozAsked:
Who is Participating?
 
nap0leonCommented:
IMO, change:
                    <select name="Platform" id="Platform">
                      <option selected>Select a Platform</option>
                      <option>PC</option>
                      <option>Mac</option>
                      <option>Server</option>
                    </select>

Open in new window


to

                    <select name="Platform" id="Platform">
                      <option selected value="">Select a Platform</option>
                      <option value="PC">PC</option>
                      <option value="Mac">Mac</option>
                      <option value="Server">Server</option>
                    </select>

Open in new window


then add a test such as
if (document.getElementById("Platform").value==""){
  //add error message here
  errors += '- Platform is required.\n'
} else{
  //they selected a platform, no error
}

Open in new window

0
 
businessesatozAuthor Commented:
where should i add the test?

<!--form Validator-->
<script type="text/javascript">
function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }
</script>

Open in new window


within this file... also
<form action="signUp-form.php" name="s" method="post" onSubmit="MM_validateForm('Name','','R','Company','','R','Email','','RisEmail');return document.MM_returnValue">

Open in new window

don't i need to change something here?
0
 
businessesatozAuthor Commented:
nevermind.. thanks i got it to work..
0
 
businessesatozAuthor Commented:
thank you for your help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.