[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to javascript validateForm

Posted on 2011-10-28
5
Medium Priority
?
220 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:businessesatoz
  • 3
5 Comments
 
LVL 83

Assisted Solution

by:leakim971
leakim971 earned 200 total points
ID: 37046692
0
 
LVL 18

Accepted Solution

by:
nap0leon earned 1800 total points
ID: 37046702
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
 

Author Comment

by:businessesatoz
ID: 37046754
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
 

Author Comment

by:businessesatoz
ID: 37046815
nevermind.. thanks i got it to work..
0
 

Author Closing Comment

by:businessesatoz
ID: 37047812
thank you for your help.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Find out what you should include to make the best professional email signature for your organization.
In this tutorial viewers will learn how to embed Flash content in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: "<!DOCTYPE html>": Use the <object> tag to embed Flash content.: To specify that the object is Flash content, d…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question