2 Replies - 717 Views - Last Post: 01 November 2013 - 01:44 PM

#1 lamentofking  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 180
  • Joined: 03-July 13

Form Validation Multiple Fields

Posted 01 November 2013 - 01:25 PM

Hello,

I am trying to do a form validation. The form consists of textboxes, checkboxes, and radio buttons. What I want to do is:

<form name="myForm" action="" method="post" onsubmit="return validate(myForm)" onsubmit="return checkbox(myForm)" onsubmit="return chkradio(myForm)" >


This isn't working though. How do I call other functions in my javascript code when submitting the form? I thought the onsubmit option for forms could take more than one parameter but I guess not.

<script type="text/javascript">

function validate()
{
var x=document.forms["myForm"]["Email"].value;
var y=document.forms["myForm"]["Name"].value;
var c=document.forms["myForm"]["Comments"].value;

var atpos=x.indexOf("@");
var dotpos=x.lastIndexOf(".");

if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
  {
  alert("Please enter a valid e-mail address");
  return false;
  }
if (y==null || y=="")
  {
  alert("Name must be filled out");
  return false;
  }
if (c==null || c=="")
  {
  alert("Comments must be filled out");
  return false;
  }

}

function checkbox()
{
var allchecked=0;
if(document.getElementById('sitedesign').checked)allchecked=1;
if(document.getElementById('links').checked)allchecked=1;
if(document.getElementById('easeofuse').checked)allchecked=1;
if(document.getElementById('images').checked)allchecked=1;
if(document.getElementById('sourcecode').checked)allchecked=1;
if(allchecked==0)alert('Very well. I\'ll be right back with the check!');
else alert('We\'ll bring that right out!');
}

function chkradio()
{
var elem=document.forms['myForm'].elements['rating'];
len=elem.length-1;
chkvalue='';
for(i=0; i<=len; i++)
{
if(elem[i].checked)chkvalue=elem[i].value;	
}
if(chkvalue=='')
{
alert('No button checked.');
return false;
}
alert ('value of checked button is '+chkvalue);
return true;
}
</script>


Is This A Good Question/Topic? 0
  • +

Replies To: Form Validation Multiple Fields

#2 DarenR  Icon User is online

  • D.I.C Lover

Reputation: 485
  • View blog
  • Posts: 3,264
  • Joined: 12-January 10

Re: Form Validation Multiple Fields

Posted 01 November 2013 - 01:30 PM

why not do one of the following:

call the function with in the variables
or call the function within the script section

in variables
<input radiob id"" name"" function();

or

function();

function your function()
{
}

Was This Post Helpful? 0
  • +
  • -

#3 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3559
  • View blog
  • Posts: 10,349
  • Joined: 08-June 10

Re: Form Validation Multiple Fields

Posted 01 November 2013 - 01:44 PM

Quote

I thought the onsubmit option for forms could take more than one parameter but I guess not.

there can be at most one event attribute (of a type) per element.

but you could of course use JS to do the work.

// pseudo code
form.addEventListener('submit', function(evt) {
  if (!validation1() || !validation2() || !validation3()) {
    evt.preventDefault();
  }
});




Quote

if (y==null || y=="")

form element values are never null.

besides that, it wouldn’t matter in a type-less comparison anyways.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1