Return to Snippet

Revision: 242
at July 6, 2006 02:59 by kaminogoya


Initial Code
<script type="text/javascript">
  function checkform(form) {
    var texts = new Array('name','email','text');
    var textnames = new Array('Name','Email','Textarea');
    var radios = new Array('radio');
    var radionames = new Array('Radio');
    var checkboxes = new Array('check');
    var checkboxnames = new Array('Checkbox');
    var i, j;
    var missing = "";
    var checkvalue = new Array();
    for (i = 0; i < texts.length; i++) {
      if (form.elements[texts[i]].value == "") {
        missing += textnames[i] + "\n";
      }
    }
    for (i = 0; i < radios.length; i++) {
      var radioelement = form.elements[radios[i]];
      for (j = 0; j < radioelement.length; j++) {
        if (radioelement[j].checked) {
          checkvalue[i] = radioelement[j].value;
          break;
        }
      }
      if (!(checkvalue[i])) {
        missing += radionames[i] + "\n";
      }
    }
    if (missing != "") {
      alert("Please input the following items.:\n" + missing);
      return false;
    }
    return true;
  }
</script>
<form method="post" action="" onsubmit="return checkform(this);">
  <fieldset>
    <legend>Test Form</legend>
    <dl>
      <dt><label for="name">Name</label></dt>
      <dd><input type="text" name="name" id="name" value="" /></dd>
      <dt><label for="email">Email</label></dt>
      <dd><input type="text" name="email" id="email" value="" /></dd>
      <dt>Radio</dt>
      <dd>
        <ul class="inline">
          <li><input type="radio" name="radio" id="Yes" value="Yes" /><label for="Yes">Yes</label></li>
          <li><input type="radio" name="radio" id="No" value="No" /><label for="No">No</label></li>
        </ul>
      </dd>
      <dt><label for="text">Textarea</label></dt>
      <dd><textarea name="text" id="text" rows="10" cols="50" onfocus="if (this.value=='Leave your messeage here.') this.value='';">Leave your messeage here.</textarea></dd>
    </dl>
    <div class="submit">
      <label for="check"><input type="checkbox" name="check" id="check" value="check" />Checkbox</label>
      <input type="submit" value="Submit" />
    </div>
  </fieldset>
</form>

Initial URL

                                

Initial Description

                                

Initial Title
form checks script

Initial Tags
form

Initial Language
JavaScript