Hello,

I am new to JavaScript, I have to make a pizza webpage for my assignment. I am done the HTML part and PHP part. I have a form and when the user clicks submit it calls the check() method in the JavaScript. Even if the fields are empty, the webpage goes to PHP page, which i do not want. If the input fields are empty, the browser should stop and wait for the user to re-enter. I have know idea what to do. Any suggestions? Thanks :)

if( a =="" || b=="" || c=="" || d==""){
   alert("Input fields can not be empty");
}

P.S. I have this due pretty soon.

Recommended Answers

function check()
{
  var a = document.getElementById('name').value;
  var b = document.getElementById('address').value;
  if( a =="" || b==""){
     alert("Input fields can not be empty");
     return false;
  }
return true;
}

Where name and address is id of the fields.

Jump to Post

All 3 Replies

function check()
{
  var a = document.getElementById('name').value;
  var b = document.getElementById('address').value;
  if( a =="" || b==""){
     alert("Input fields can not be empty");
     return false;
  }
return true;
}

Where name and address is id of the fields.

Its not working :(

function check(){
		var d=confirm("Are you sure you want to place your order?");
		var a= myForm.name.value;
		var b= myForm.phoneNumber.value;
		var c= myForm.unitNumber.value;
		var d= myForm.streetName.value;
		var e= myForm.city.value;
		var f= myForm.zip.value;
		var g= myForm.province.value;
		var count;
		isEmpty(a,b,c,d,e,f,g);

                function isEmpty(a,b,c,d,e,f,g){
	if( a =="" || b=="" || c=="" || d=="" || e=="" || f=="" || g==""){
					alert("Input fields can not be empty");
					return false;
				} 
				return true;
			}
}

- you can not define function in another function.
- always keep practice to return value from any function.

<script language="javascript">
function check()
{
	if(confirm("Are you sure you want to place your order?"))
	{
		var a= document.myForm.name.value;
		var b= document.myForm.phoneNumber.value;
		var c= document.myForm.unitNumber.value;
		var d= document.myForm.streetName.value;
		var e= document.myForm.city.value;
		var f= document.myForm.zip.value;
		var g= document.myForm.province.value;
		if( a =="" || b=="" || c=="" || d=="" || e=="" || f=="" || g=="")
		{
			alert("Input fields can not be empty");
			return false;
		}
		else
			return true;
	}
	else
	{
		return false;
	}
} 
</script>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.