PDA

View Full Version : Required field(s) validation



aolson
11-21-2005, 09:32 PM
Script Required field(s) validation
http://www.dynamicdrive.com/dynamicindex16/requiredcheck.htm
This does not appear to work with a mailto: command. Please tell me what I am doing wrong.
Here is my form code at the top in the body tag I have the script.
Thanks for any help. Please e-mail me or post back to this forum.


<form action="mailto:help@gothiclandscape.com" method="post" enctype="text/plain" name="ITOrder" onsubmit="return ITOrder(this);">
<table width="427" border="1">
<tr>
<td width="161">What user is this for?</td>
<td width="84"><strong>First Name</strong></td>
<td width="160"><input name="firstname" type="text" id="firstname"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><strong>Last Name </strong></td>
<td><input name="lastname" type="text" id="lastname"></td>
</tr>
</table>
<input name="submit" type="submit" value="submit">
</form>

Twey
11-22-2005, 04:21 PM
By the fact that your function name isn't the same as in the original script. From this I deduce that you've probably changed it in some way. If this is the case, my grandmother is half a goose. If not, you need to use exactly the code supplied:

onsubmit="return formCheck(this);"

aolson
11-22-2005, 05:56 PM
I changed the function name to formcheck and it still bypasses the checking and begins to process sending the e-mail. This is a intranet site so I cannot send you the link.

Twey
11-22-2005, 05:58 PM
In that case, please post your page code here, inside [code] tags.

aolson
11-22-2005, 09:09 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script language="JavaScript">
<!--

/***********************************************
* Required field(s) validation v1.10- By NavSurf
* Visit Nav Surf at http://navsurf.com
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("FirstName", "LastName");
// Enter field description to appear in the dialog box
var fieldDescription = Array("First Name", "Last Name");
// dialog message
var alertMsg = "Please complete the following fields:\n";

var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired[i]];
if (obj){
switch(obj.type){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
case "text":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\n";
}
break;
default:
}
if (obj.type == undefined){
var blnchecked = false;
for (var j = 0; j < obj.length; j++){
if (obj[j].checked){
blnchecked = true;
}
}
if (!blnchecked){
alertMsg += " - " + fieldDescription[i] + "\n";
}
}
}
}

if (alertMsg.length == l_Msg){
return true;
}else{
alert(alertMsg);
return false;
}
}
// -->
</script>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
</head>

<body>
<form action="mailto:help@gothiclandscape.com" method="post" enctype="text/plain" name="ITOrder" onsubmit="return formcheck(this);">
<table width="427" border="1">
<tr>
<td width="161">What user is this for?</td>
<td width="84"><strong>First Name</strong></td>
<td width="160"><input name="firstname" type="text" id="firstname"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><strong>Last Name </strong></td>
<td><input name="lastname" type="text" id="lastname"></td>
</tr>
</table>
<input name="submit" type="submit" value="submit">
</form>

</body>
</html>

Twey
11-22-2005, 09:15 PM
Very simple error: it's formCheck(this), not formcheck(this). Case is important.

<form action="mailto:help@gothiclandscape.com" method="post" enctype="text/plain" name="ITOrder" onsubmit="return formCheck(this);">

aolson
11-22-2005, 09:28 PM
I changed the case to
<form action="mailto:help@gothiclandscape.com" method="post" enctype="text/plain" name="ITOrder" onsubmit="return formCheck(this);"> but when I open this in Internet Explorer with no value in the First Name and Last Name fields it does not prompt saying please complete the following fields.
What am I doing wrong?
Thanks for all your help.