0

I have a form having validation given below in java script.

The problem is when i am inserting form data in a database,if there is any validation error,still added in database.

please give me suggestion where to use PHP code
I am using this code for insertion

<?php
if(isset($_POST['submit']))
 {
     $query="INSERT INTO requirement (name, email, mobile, gender,roomtype, pricefrom, priceto ,city, location, requirment) VALUES ('$_POST[name]','$_POST[frmemail]','$_POST[mobile]','$_POST[gender]','$_POST[roomtype]','$_POST[pricefrom]','$_POST[priceto]','$_POST[city]','$_POST[location]','$_POST[requirement]')";
     $result=mysql_query($query);
     echo '<script language="javascript">alert("Thanks for contacting us!")</script>;'; 
 }
?>

JS code:()use that code on onsubmit() method

// JavaScript Document

String.prototype.trim = function() 
{ var _ret = this.replace(/^\s+|\s+$/g, ''); 
return _ret;//this.replace(/^(\&nbsp\;)+|(\&nbsp\;)+$/g, ''); 
}

function isNumeric(str)
{ str=str.replace(/^\s+|\s+$/g, '') ;
if((str.length == 1 && str.match(/^\d+$/g)) || ((str.length > 1) && str.match(/^[-]{0,1}\d*[.]{0,1}\d*$/g))) return true;
return false;
}


function check_form(theform)
{

	if(theform.name.value=="")
	{
		alert("Please enter Name !!");
		theform.name.focus();
		return false;
	}


	if(theform.frmemail.value=="")
	{
		alert("Please enter E-Mail !!");
		theform.frmcoperson.focus();
		return false;
	}
	
	if(theform.mobile.value=="")
	{
		alert("Please enter Mobile Number !!");
		theform.mobile.focus();
		return false;
	
	}
	
	
	if(theform.pricefrom.value=="")
	{
		alert("Please enter Price-from !!");
		theform.pricefrom.focus();
		return false;
	
	}
	if(theform.priceto.value=="")
	{
		alert("Please enter Price-to !!");
		theform.priceto.focus();
		return false;
	
	}
	
	if(theform.city.value=="")
	{
		alert("Please enter City !!");
		theform.city.focus();
		return false;
	}
	
	if(theform.location.value=="")
	{
		alert("Please enter Location!!");
		theform.location.focus();
		return false;
	}
	
	if(theform.requirement.value=="")
	{
		alert("Please enter Requirement !!");
		theform.requirement.focus();
		return false;
	}


	if(theform.frmemail.value.indexOf(" ") >= 0)
	{
		alert("Please enter your email-id without any white space character.");
		theform.frmemail.focus();
		return (false);
	}
	
	if ( (theform.frmemail.value.indexOf("@") == -1) || (theform.frmemail.value.indexOf(".") == -1) )
	{
		alert("Please enter a valid email-id"); 
		theform.frmemail.focus();
		return (false);
	}
	
	
	BeforeAtRate = theform.frmemail.value.substring(0,theform.frmemail.value.indexOf("@"));
	AfterAtRate = theform.frmemail.value.substring(theform.frmemail.value.indexOf("@")+1,theform.frmemail.value.length);
	
	if (AfterAtRate.indexOf(".") == -1)
	{
		alert("Please enter a valid email-id"); 
		theform.frmemail.focus();
		return (false);
	}
	
	middle = AfterAtRate.substring(0, AfterAtRate.indexOf("."));
	last = AfterAtRate.substring(AfterAtRate.indexOf(".") + 1,AfterAtRate.length);
	
	if (BeforeAtRate.length == 0 || middle.length == 0 || last.length == 0)
	{
		alert("Please enter a valid email-id"); 
		theform.frmemail.focus();
		return (false);
	}

	return true;
}
2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by cereal
0

Add a new var on line 17:

var check = true;

And for each if statement, replace return (false); with check = false; At the end, where you wrote return true; write:

if(check == true)
{
    return true;
    // send form
}

else
{
    return false;
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.