1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Javascript onSubmit not working

Discussion in 'JavaScript' started by relixx, Sep 28, 2005.

  1. #1
    Basically, in the form my onSubmit looks like this:

    onSubmit="return validate_form()"

    This would, logically, submit the form if 'true' was return, and cancel the submition is 'false' was returned. However, it's simply not doing this, and it's starting to frustrate me. There isn't an error code being generated in Fx or IE, so my code seems to be correct.

    
    function validate_form()
    {
    var pass = true
    	if (!checkRequiredFields())
    	{
    	document.email_form.Name_surname.focus();
    	pass = false
    	}
    
    	if (!checkemail())
    	{
    	document.email_form.email.focus();
    	pass = false
    	}
    	
    	
    	if(!checkRoomTotals())
    	{
    	pass = false
    	}
    
    return pass
    
    
    }
    
    function checkRequiredFields()
    {
    
    strNameSurname = document.email_form.Name_surname.value
    strCountry = document.email_form.Country.value
    strEmail = document.email_form.email.value
    dteDateArrive = document.email_form.Date_Arrive.value
    dteDateDepart = document.email_form.Date_Depart.value
    
    
    
    	if (strNameSurname == "" || strCountry == "" || strEmail == "" || dteDateArrive == "" || dteDateDepart == "")
    	{
    		alert("One or more of the required fields are not completed. Please complete them, then submit again!");
    		return false;
    	}
    	else
    	{
    		return true
    	}
    }
    
    
    function checkBlank(strString)
    {
    
    	switch(strString)
    	{
    	case 'Name_surname':
    		var x=document.email_form.Name_surname.value;
    		x = x.replace(/\s*$/,"")
    		if (x=="")
    		{
    		alert("You may not leave the 'Name and Surname' field blank!");
    		return false;
    		}
    		break;
    	case 'Country':
    		var x=document.email_form.Country.value;
    		x = x.replace(/\s*$/,"")
    		if (x=="")
    		{
    		alert("You may not leave the 'Country' field blank!");
    		return false;
    		}
    		break;
    	case 'email':
    		var x=document.email_form.email.value;
    		x = x.replace(/\s*$/,"")
    		if (x=="")
    		{
    		alert("You may not leave the 'Email' field blank!");
    		return false;
    		}
    		break;
    	case 'Date_Arrive':
    		var x=document.email_form.Date_Arrive.value;
    		x = x.replace(/\s*$/,"")
    		if (x=="")
    		{
    		alert("You may not leave the 'Date Arrive' field blank!");
    		return false;
    		}
    		break;
    	case 'Date_Depart':
    		var x=document.email_form.Date_Depart.value;
    		x = x.replace(/\s*$/,"")
    		if (x=="")
    		{
    		alert("You may not leave the 'Date Depart' field blank!");
    		return false;
    		}
    		break;
    	default:
    		alert("Invalid Object name, Einstein!");
    		return false;
    		break;
    	}
    
    }
    
    function checkRoomTotals()
    {
    var intSeaViewAdult = parseInt(document.email_form.Sea_View_Adult.value)
    var intSeaViewChild = parseInt(document.email_form.Sea_View_Child.value)
    var intBayViewAdult = parseInt(document.email_form.Bay_View_Adult.value)
    var intBayViewChild = parseInt(document.email_form.Bay_View_Child.value)
    var intMntViewAdult = parseInt(document.email_form.Mnt_View_Adult.value)
    var intMntViewChild = parseInt(document.email_form.Mnt_View_Child.value)
    var x = intSeaViewAdult + intSeaViewChild + intBayViewAdult + intBayViewChild + intMntViewAdult + intMntViewChild
    
    	if (x == 0)
    	{
    	alert("You must book at least 1 person!");
    	return false;
    	}
    	
    	if (x > 6)
    	{
    	alert("You are allowed a maximum of 6 people per booking!");
    	return false;
    	}
    	
    	if ((intSeaViewAdult + intSeaViewChild > 2) || (intBayViewAdult + intBayViewChild > 2) || (intMntViewAdult + intMntViewChild > 2))
    	{
    	alert("You are allowed a maximum of 2 people per room!");
    	return false;
    	}
    	
    
    }
    //Advanced Email Check credit-
    //By JavaScript Kit (http://www.javascriptkit.com)
    //Over 200+ free scripts here!
    
    function checkemail()
    {
    	var str=document.email_form.email.value
    	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    	if (filter.test(str))
    	{
    		return true;
    	}
    	else
    	{
    		alert("Please enter a valid email address!");
    		return false
    	}
    
    }
    
    Code (markup):
    can anyone help me?
     
    relixx, Sep 28, 2005 IP
  2. relixx

    relixx Active Member

    Messages:
    946
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    70
    #2
    Ok, here is the code that I've been testing with:
    
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    <script language="javascript">
    function validate_form()
    {
    var pass = true
    	if (!checkRequiredFields())
    	{
    	document.email_form.Name_surname.focus();
    	pass = false
    	}
    
    	if (!checkemail())
    	{
    	document.email_form.email.focus();
    	pass = false
    	}
    	
    	if(!checkRoomTotals())
    	{
    	pass = false
    	}
    
    return pass
    
    
    }
    
    function checkRequiredFields()
    {
    
    strNameSurname = document.email_form.Name_surname.value
    strCountry = document.email_form.Country.value
    strEmail = document.email_form.email.value
    dteDateArrive = document.email_form.Date_Arrive.value
    dteDateDepart = document.email_form.Date_Depart.value
    
    
    
    	if (strNameSurname == "" || strCountry == "" || strEmail == "" || dteDateArrive == "" || dteDateDepart == "")
    	{
    		alert("One or more of the required fields are not completed. Please complete them, then submit again!");
    		return false;
    	}
    	else
    	{
    		return true
    	}
    }
    
    
    
    
    function checkRoomTotals() //checks the totals been booked into the rooms. max: 2 per room, 6 per booking
    {
    var intSeaViewAdult = parseInt(document.email_form.Sea_View_Adult.value) //change string value to an integer
    var intSeaViewChild = parseInt(document.email_form.Sea_View_Child.value) //change string value to an integer
    var intBayViewAdult = parseInt(document.email_form.Bay_View_Adult.value) //change string value to an integer
    var intBayViewChild = parseInt(document.email_form.Bay_View_Child.value) //change string value to an integer
    var x = intSeaViewAdult + intSeaViewChild + intBayViewAdult + intBayViewChild
    
    	if (x == 0)
    	{
    	alert("You must book at least 1 person!");
    	return false;
    	}
    	
    	if (x > 6)
    	{
    	alert("You are allowed a maximum of 6 people per booking!");
    	return false;
    	}
    	
    	if ((intSeaViewAdult + intSeaViewChild > 2) || (intBayViewAdult + intBayViewChild > 2))
    	{
    	alert("You are allowed a maximum of 2 people per room!");
    	return false;
    	}
    	
    
    }
    //Advanced Email Check credit-
    //By JavaScript Kit (http://www.javascriptkit.com)
    //Over 200+ free scripts here!
    
    function checkemail()
    {
    	var str=document.email_form.email.value
    	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
    	if (filter.test(str))
    	{
    		return true;
    	}
    	else
    	{
    		alert("Please enter a valid email address!");
    		return false
    	}
    
    }
    </script>
    </head>
    
    <body>
    <form method="post" name="email_form" onSubmit="return validate_form()" action="mailto:relixx@gmail.com">
    Name and surname: <input type="text" name="Name_surname"><br>
    Email:  <input type="text" name="email"><br>
    Country: <input type="text" name="Country"><br>
    Date Arrive: <input type="text" name="Date_Arrive"><br>
    Date Depart: <input type="text" name="Date_Depart"><br>
    Sea View Adult: 
    	<select name="Sea_View_Adult">
    	<option value="0">0</option>
    	<option value="1">1</option>
    	<option value="2">2</option>
    	</select><br>
    Sea View Children: 
    	<select name="Sea_View_Child">
    	<option value="0">0</option>
    	<option value="1">1</option>
    	<option value="2">2</option>
    	</select><br>
    Bay View Adult: 
    	<select name="Bay_View_Adult">
    	<option value="0">0</option>
    	<option value="1">1</option>
    	<option value="2">2</option>
    	</select><br>
    Bay View Children: 
    	<select name="Bay_View_Child">
    	<option value="0">0</option>
    	<option value="1">1</option>
    	<option value="2">2</option>
    	</select><br>
    <input type="Submit" value="Submit" name="Submit">
    </form>
    </body>
    </html>
    
    
    Code (markup):
    Now, by commenting out each function in order I've narrowed it down to the checkRoomTotals() function as the cause of the problem. For some reason, it's preventing the form from being submitted.

    
    function checkRoomTotals() //checks the totals been booked into the rooms. max: 2 per room, 6 per booking
    {
    var intSeaViewAdult = parseInt(document.email_form.Sea_View_Adult.value) //having to change string value to and integer
    var intSeaViewChild = parseInt(document.email_form.Sea_View_Child.value) //having to change string value to and integer
    var intBayViewAdult = parseInt(document.email_form.Bay_View_Adult.value) //having to change string value to and integer
    var intBayViewChild = parseInt(document.email_form.Bay_View_Child.value) //having to change string value to and integer
    var x = intSeaViewAdult + intSeaViewChild + intBayViewAdult + intBayViewChild
    
    	if (x == 0)
    	{
    	alert("You must book at least 1 person!");
    	return false;
    	}
    	
    	if (x > 6)
    	{
    	alert("You are allowed a maximum of 6 people per booking!");
    	return false;
    	}
    	
    	if ((intSeaViewAdult + intSeaViewChild > 2) || (intBayViewAdult + intBayViewChild > 2))
    	{
    	alert("You are allowed a maximum of 2 people per room!");
    	return false;
    	}
    
    }
    
    Code (markup):
    Please, does anyone have any suggestions to what I'm doing wrong?
     
    relixx, Sep 28, 2005 IP
  3. rederick

    rederick Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    function checkRoomTotals() //checks the totals been booked into the rooms. max: 2 per room, 6 per booking
    {
    var intSeaViewAdult = parseInt(document.email_form.Sea_View_Adult.value) //having to change string value to and integer
    var intSeaViewChild = parseInt(document.email_form.Sea_View_Child.value) //having to change string value to and integer
    var intBayViewAdult = parseInt(document.email_form.Bay_View_Adult.value) //having to change string value to and integer
    var intBayViewChild = parseInt(document.email_form.Bay_View_Child.value) //having to change string value to and integer
    var x = intSeaViewAdult + intSeaViewChild + intBayViewAdult + intBayViewChild
    
    	if (x == 0)
    	{
    	alert("You must book at least 1 person!");
    	return false;
    	}
    	
    	if (x > 6)
    	{
    	alert("You are allowed a maximum of 6 people per booking!");
    	return false;
    	}
    	
    	if ((intSeaViewAdult + intSeaViewChild > 2) || (intBayViewAdult + intBayViewChild > 2))
    	{
    	alert("You are allowed a maximum of 2 people per room!");
    	return false;
    	}
      [COLOR="Red"]return true;[/COLOR]
    }
    
    Code (markup):
    In your function checkRoomTotals() you never "return true" ...
    That might fix it for you...
     
    rederick, Sep 30, 2005 IP
  4. relixx

    relixx Active Member

    Messages:
    946
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    70
    #4
    Yeah, that was pointed out by a friend of mine yesterday. I felt so stupid :eek: I always overlook the obvious things :(
     
    relixx, Oct 1, 2005 IP