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

Recommended Answers

All 4 Replies

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.

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

that works great, thanks hielo

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

Regards,
Hielo

Be a part of the DaniWeb community

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