Hi
Have a look at this. Its is completely dynamic and you can specify in variables what is required and what not & Numeric etc. In this case I have added a popup to show which values are not entered. I uncommented the alerts. I got this off the net so cheers to that guy:
On the form call this function on when the button is pressed:
<SCRIPT language="JavaScript">
function Validate() {
// define a variable to hold an error message.
var strErrorMessage = String("") ;
var varPassed ;
var varReasonFailed ;
// Get a handle to our form object.
var f = form1 ;
// Update the value in our hidden field, so that we know what
// button was pressed.
// f.hidAction.value = s.name ;
// Declare which fields are optional.
f.Reset.optional = true ;
//n is for the
vb split function on required.asp...know how many splits to do??:)
n = 0;
// Declare which fields are supposed to be numeric.
f.ClientTel.numeric = true ;
f.TotalOppVal.numeric = true ;
f.DCVal.numeric = true ;
f.PotRevenue.numeric = true ;
// Iterate through the elements on our form and validate them
// as we go.
for(var i=0; i<=f.elements.length-1; i++) {
varPassed = "PASSED" ;
varReasonFailed = "" ;
// Get a handle to the current element.
var e = f.elements[i] ;
// We'll store the value of the element in a variable.
var ElementValue = "" ;
// Use a switch statement to work out what type of element
// we have, because we get the value of different elements
// in differing ways.
switch(e.type) {
case "text":
ElementValue = e.value ;
break ;
case "select-one":
ElementValue = e[e.selectedIndex].id ;
break ;
case "textarea":
ElementValue = e.value ;
break ;
}
// OK, we're not interested in Hidden Fields,Buttons,
// or Optional fields so skip them
if( e.type != "hidden" && e.type != "button" && !e.optional )
{
// Once we get here we need to make sure that the user has entered some
// sort of entry (in other words - make sure that the field isn't empty).
if ( ElementValue == null || ElementValue == "" ) {
// If the field is empty then we append a message to our Error Message
// string.
strErrorMessage += e.id + ".\n" ;
varPassed = "FAILED" ;
n = n + 1;
varReasonFailed += "Because no value was entered.\n" ;
}
}
// Now validate the fields that are supposed to be numeric.
if(e.numeric == true) {
if( isNaN(ElementValue) ) {
// The user entered a non-numeric value where a
// numeric value was required.
strErrorMessage += "Enter a numeric value for " + e.id + ".\n" ;
varPassed = "FAILED" ;
n = n + 1;
varReasonFailed += "Because the value entered was not numeric.\n" ;
}
}
if(e.numeric == false) {
if( !isNaN(ElementValue) && ElementValue != null && ElementValue != "" ) {
// The user entered a non-numeric value where a
// numeric value was required.
strErrorMessage += "You cannot enter a numeric value for " + e.id + ".\n" ;
varPassed = "FAILED" ;
n = n + 1;
varReasonFailed += "Because the value entered was numeric." ;
}
}
var strDetails = ""
strDetails += "VALIDATING: " + e.id + "\n"
strDetails += "-------------------------------------------\n\n"
strDetails += "TYPE: " + e.type + ",\n"
strDetails += "VALUE: " + ElementValue + ",\n"
strDetails += "NUMERIC: " + e.numeric + ",\n"
strDetails += "OPTIONAL: " + e.optional + ".\n\n"
strDetails += "This element " + varPassed + " the validation\n"
strDetails += varReasonFailed ;
//alert(strDetails) ;
}
// If there is an error message, display it to the user, otherwise,
// submit the form.
if (strErrorMessage.length >= 1) {
msgWindow = window.open('','newWin','width=400,height=400,scre enX=400,screenY=400,top=200,left=350');
//open the window and pass the error and count of errors
msgWindow.location.href = 'required.asp?Error=' + strErrorMessage + "&Count=" + n ;
} else {
f.submit() ;
}
}
</script>
The Popup
<%
ErrorD = request.QueryString("Error")
i = request.QueryString("Count")
Splt = split(ErrorD,".")
a = 0
%>
<table align="center">
<tr><td bgcolor="#CCCCCC">Required Fields not Entered</td></tr>
<%
for b = 0 TO i
z = Splt(a)
%>
<tr><td align="left"><%response.write(Z)%></td></tr>
<%
a = a + 1
next
%>
<tr><td align="center"><input name="Close" align="middle" type="button" value="Close" onClick='window.close()';></td></tr>
</table>
Hope this helps
Regards
Marnus