Hi, I have setup a simple contact formin HTML which validates the fields that are required using javascript. Here is my javascript:
function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("Name", "Email", "Enquiry", "Security");
// Enter field description to appear in the dialog box
var fieldDescription = Array("Name", "Email", "Enquiry", "Security");
// dialog message
var alertMsg = "Please complete the following fields:\
";
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] + "\
";
}
break;
case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " - " + fieldDescription[i] + "\
";
}
break;
case "text":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription[i] + "\
";
}
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] + "\
";
}
}
}
}
if (alertMsg.length == l_Msg){
return true;
}else{
alert(alertMsg);
return false;
}
}
I have a field in my contact form called security. What I am trying to achieve is when you input the number 5 into the security field the form will go ahead and work, but if you input any other number the form will not work and popup an error. - How can I implement this?
Here is my HTML code for the form:
<form name="formcheck" action="/enquiry.php" onsubmit="return formCheck(this);">
< table align="center" border="0" cellpadding="8" cellspacing="0" width="50%">
<tr>
<td align="left">Name:</td>
<td colspan="2" align="left"><input name="Name" type="text" id="Name" size="14" /></td>
</tr>
<tr>
<td align="left">Email:</td>
<td colspan="2" align="left"><input name="Email" type="text" id="Email" size="14" /></td>
</tr>
<tr>
<td align="left" valign="top">Enquiry:</td>
<td colspan="2" align="left" valign="top"><textarea name="Enquiry" id="Enquiry" style="width:105px;height:75px;" rows="" cols=""></textarea></td>
</tr>
<tr>
<td align="left" valign="top">Security:</td>
<td align="right" valign="top">1 + 4 = <input name="Security" type="text" id="Security" size="1" maxlength="1" /></td>
</tr>
<tr>
<td colspan="3">
<div align="right">
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />
</div></td>
</tr>
</table>
</form>
Any help would be very much appreciated. Thanks