Hi,
I have this code which is not working

<script type="text/javascript" language="javascript">
<!--
function loadpage() {
	document.getElementById('Loadingindic').style.top = '-500%';
}

function checkfields() {
	if(name()==true){
		if(email()==true){
			if(message()==true){
				return true;
			}
		}
	}
	return false
}

function name() {
	name = document.getElementById('CF_N').value;
	if ( name.length == 0 ) {
		alert('Please enter your name!');
		return false;
	} else {
		return true;
	}
}

function message() {
	name = document.getElementById('CF_M').value;
	if ( name.length == 0 ) {
		alert('Please enter a message!');
		return false;
	} else {
		return true;
	}
}

function email() {
	name = document.getElementById('CF_E').value;
	var validchars = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	if(name.match(validchars)){
		return true;
	}else{
		alert('Please enter a valid email!');
		return false;
	}
}
//-->
</script>

When a form is submitted, checkfields() is run. The first time it is run it runs correctly, picking up on invalid fields but after that it just submits the form without paying attention to the validation. What is wrong?
Regards,
Sam Rudge

this means there is some problem in your javascript. when the first time the javascript called its check and an javascript error occur and when you click again then the existing error return true to the form that's why the form is submitted.

Regards:
<URL SNIPPED>Nasir

Here's the modified version of your code:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html id="xhtml10S" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://www.w3.org/2005/10/profile">
<!--[if IE]>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<![endif]-->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="Content-Style-Type" content="text/css" />
<meta http-equiv="Content-Script-Type" content="text/javascript" />
<title>Free Live Help!</title>
<script type="text/javascript">
<!--
var checkFields = function( form ) {
var validChars = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
   
   var name1 = form.CF_N;
   var name2 = form.CF_M;
   var name3 = form.CF_E;
   var nLen = name1.value.length;
   var mLen = name2.value.length;
   if (( nLen ) && ( mLen ) && ( validChars.test( name3.value ))) {
      alert("\nAll fields has been validated succefully, press ok/yes to continue.");
      return true;
   } for ( var x = 0; x < form.elements.length; x++ ) {
        if ( !( Boolean( form.elements[ x ].value ))) {
        var labels = (( x === 0 ) ? "Name" : (( x === 1 ) ? "Email" : "Message" ));
        alert( "This is not a valid (" + form.elements[ x ].value + ") " + labels + ", please repeat this entry." );
        form.elements[ x ].value = "";
        continue; }
   } return false;
};
 
var loadpage = function() { 
((document.getElementById ) ? document.getElementById('Loadingindic') : document.all.Loadingindic ).style.top = "-500%";
};

window.onload = loadpage;
//-->
</script>
</head>
<body>
<div id="Loadingindic">
<form id="testform" name="testform" action="#" onsubmit="return checkFields( this );">
<div>
<label for="CF_N">NAME: <input type="text" id="CF_N" name="CF_N" value="" size="30" /></label><br /><br />
<label for="CF_E">EMAIL: <input type="text" id="CF_E" name="CF_E" value="" size="30" /></label><br /><br />
<label for="CF_M">MESSAGE: <textarea id="CF_M" name="CF_M" cols="50" rows="5"></textarea></label>
<br /><br />
<input id="sbm" name="sbm" type="submit" value="Submit Form">
</div> 
</body>
</html>

Try to run the whole document first, before you make any modification.

Let me know if you still have questions regarding this code...

essential

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.