0

Hi there,
Got a javscript problem that im working on where the validation doesn't do what I think it should.

From stepping through the code, when the pound and pence fields on the form are blank, but the name is filled in, it should error at "Please fill in value for pence" but it doesnt? It just submits the form anyway?

function checkAdd(){
	
	var cname = document.getElementById('addname');
	var cpound = document.getElementById('addpound');
	var cpence = document.getElementById('addpence');
	var div = document.getElementById('errormsg');
	var lets = /^[a-zA-Z\s]+$/;
	var nums = /^[0-9]+$/;
	
	if((cname.value == '') || (cname.value == ' ')){
		div.innerHTML= "<b>Please fill in the item name</b>";
		cname.focus();
		return false;
	}
	else if(cname.value.match(lets)){
		if(cpound.value == ''){
			cpound == 0}
				else if(cpound.value.match(nums)){
					if((cpence.value == '') || (cpence.value == ' ')){
						div.innerHTML= "<b>Please fill in value for pence</b>";
						cpence.focus();
						return false;}
							else if((cpound.value.length == 1) && (cpence.value.length == 1)){
								div.innerHTML= "<b>Please enter 2 digits for pence</b>";
										cpence.focus();
										return false;}
									else if(cpence.value.match(nums)){
										return true;}
									else{
										div.innerHTML= "<b>Only numbers please</b>";
										cpence.focus();
										return false;}}
				else{
					div.innerHTML= "<b>Only numbers please</b>";
					cpound.focus();
					return false;}}
	else{
	div.innerHTML= "<b>Only letters please</b>";
	cname.focus();
	return false;}
		return true;
}
<form action="additem.php" method="post" onSubmit="return checkAdd()" > 
<table width="200" border="0" cellpadding="2" cellspacing="0" class="adforms">
<tr>
<th width="40">Name: </th>
<td width="152" align="left"><input type = "text" name = "name" size="20" id="addname" /></td>
</tr>
<tr>
<th>Price:  £</th>
<td align="left"><input type = "text" name = "pound" id="addpound" size="2" maxlength="1"/>.<input type = "text" name = "pence" id="addpence" size="2" maxlength="2"/> p</td>
</tr>
<tr>
<th>New: </th>
<td align="left"><input type = "checkbox" name = "newchk" value="1"/></td>
</tr>
<tr>
<td></td>
<td align="right"><input type="submit" value="Add item"/></td>
</tr>
</table>
</form>

Thanks in advance

Edited by ben.marks: n/a

2
Contributors
4
Replies
6
Views
6 Years
Discussion Span
Last Post by hielo
0

when the pound and pence fields on the form are blank, but the name is filled in

Not from what you coded. The problem is that when the pound is left blank, this condition is true:

if(cpound.value == '')
		{
			cpound == 0
		}

and the rest of your code ( within the else if-else ) will NOT execute, eventually leading to the "return true". Hence the reason it is submitting.

0

try:

function checkAdd(){
	
	var cname = document.getElementById('addname');
	var cpound = document.getElementById('addpound');
	var cpence = document.getElementById('addpence');
	var div = document.getElementById('errormsg');
	var lets = /^[a-zA-Z\s]+$/;
	var nums = /^[0-9]+$/;
	
	if((cname.value == '') || (cname.value == ' '))
	{
		div.innerHTML= "<b>Please fill in the item name</b>";
		cname.focus();
		return false;
	}
	else if(cname.value.match(lets))
	{
	/*
		if(cpound.value == '')
		{
			cpound == 0
		}
/*		
		if(cpound.value.match(nums))
		{
			if((cpence.value == '') || (cpence.value == ' '))
			{
				div.innerHTML= "<b>Please fill in value for pence</b>";
				cpence.focus();
				return false;
			}
			else if((cpound.value.length == 1) && (cpence.value.length == 1))
			{
				div.innerHTML= "<b>Please enter 2 digits for pence</b>";
				cpence.focus();
				return false;
			}
			else if(cpence.value.match(nums))
			{
				return true;
			}
			else
			{
				div.innerHTML= "<b>Only numbers please</b>";
				cpence.focus();
				return false;
			}
		}
		else
		{
				div.innerHTML= "<b>Only numbers please</b>";
				cpound.focus();
				return false;
		}
	}
	else
	{
		div.innerHTML= "<b>Only letters please</b>";
		cname.focus();
		return false;
	}
return true;
}
0

Line 23 should have been */. I was trying to comment out lines 18-23.

Regards,
Hielo

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.