JavaScript Validation Help

Soldato
Joined
30 Nov 2005
Posts
3,084
Location
London
Anyone know why this won't work.

Seems all valid to me.

Oh and yes I know it's old school JavaScript, it's a legacy form.

PHP:
			{
var chks = document.getElementsByName('previous');
var checkCount = 0;
for (var i = 0; i < chks.length; i++)
{
if (chks[i].checked)
{
checkCount++;
}
}
if (checkCount < 1)
{
alert("MESSAGE 1 GOES HERE.");
return false;
}
		}
		
if(contactform.previous[0].checked){
if(contactform.refnumber.value.length==0||contactform.refnumber.value==" "){ alert("MESSAGE 2 GOES HERE"); contactform.refnumber.focus(); return false;}
}
 
Where is the rest of it and what is the error message?

I guess there is some function definition at the top that is cut off, also can I see the contact form definition code?
 
Ok just had a look through the code, it's a mess btw, and it would seem there was two separate functions of the same name, being run when the onsubmit="return etc etc" was run.

So I removed one of them and the above now works.

But really need to get that piece of validation back in, which is this:

PHP:
if (!document.contactform.CODEnumber.disabled) { 

var re1letter4digit=/(?:[a-zA-Z])\d{4}/ //regular expression defining a 5 digit number
if (document.contactform.CODEnumber.value.search(re1letter4digit)==-1) { //if match failed
alert("Please enter a valid CODE number.");
return (false);

}
}

I've tried inserting the above code into the original function (which the one in my first post is in) but it won't run the above.
 
When you say it wont run what do you mean?
It doesn't get past the first if condition? You think the data you entered isn't valid but you don't get a message?

that's a slightly strange regex expression though.
it's not actually looking for a 5 digit number as suggested by the comment, its looking for a ":", a letter and then 4 letter d's in a row. so :Adddd is valid, probably not what you want!
 
The script as a whole runs, but it doesn't alert if the user doesn't enter a number that the regex expression states is required.

It's so confusing.

It's for a field box that isn't enabled as default, the field box only becomes available to enter information when a user checks a tickbox.
 
Well without seeing the rest of your code and how the form is written it's going to be difficult to debug. remember though that the document.contactform.CODEnumber only works if the name attribute is there, personally I use the document.getElementById() function.
Anyway, this bit of code works using your snippet above.

Code:
<html>
<head>
<script>
function fnDoIt(){
	if (!document.contactform.CODEnumber.disabled) { 
		var re1letter4digit=/\d{5}/ //regular expression defining a 5 digit number
		if (document.contactform.CODEnumber.value.search(re1letter4digit)==-1) { //if match failed
			alert("Please enter a valid CODE number.");
			return (false);
			}
		else
		{
			alert('Code Valid');
		}
	}  
}
function fnchangedisabled(){
	document.getElementById('CODEnumber').disabled = !document.getElementById('CODEnumber').disabled
}
</script>
</head>
<body onload="javascript:document.getElementById('CODEnumber').disabled=true;">
	<form name="contactform" id="contactform" onsubmit=javascript:fnDoIt();>
		<input type=text name="CODEnumber" id="CODEnumber"></input>
		<input type=submit>
	</form>
	<form id=switch>
		<input type=checkbox id="chkSwitch" onclick="javascript:fnchangedisabled();" >Switch</input>
	</form>
</body>
</html>
 
Last edited:
Back
Top Bottom