Form Validates Required Input Before Submit

[Total: 0    Average: 0/5]

Only forms with required number of input completed will be submitted.

Head Code:

<script>
function val()
{
  var _ret=true; /* false means not validated */
  var _m="Your input number must be provided by equal number of items";

  var _f=document.f1;
  var _n=parseInt(_f.n.value);
  switch(_n)
  {
    case 5 : if(!_f.t5.value.length)_ret=false;
    case 4 : if(!_f.t4.value.length)_ret=false;
    case 3 : if(!_f.t3.value.length)_ret=false;
    case 2 : if(!_f.t2.value.length)_ret=false;
    case 1 : if(!_f.t1.value.length)_ret=false;
             break;
    default : 
      _m="Input number is not valid number:n'"+_n+"'nn";
      _ret=false;
  }
  if(!_ret)alert(_m);
  return _ret;
}
</script>

Body Code:

<form name=f1 onsubmit="return val()">
Number:<input type=text name=n><BR>
One:<input name=t1><BR>
Two:<input name=t2><BR>
Three:<input name=t3><BR>
Four:<input name=t4><BR>
Five:<input name=t5><BR>
<input type=submit>
</form>

Times Viewed: 4

Leave a Reply

Your email address will not be published. Required fields are marked *