0

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.

2
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by vibhaJ
0
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.

Edited by vibhaJ: n/a

0

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;
			}
}
0

- 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>
This question has already been answered. 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.