Forum Moderators: open
I have coded a form with some required fields which are validated onSubmit using code from [dynamicdrive.com...] ie.
function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("Title", "Name", "Company", "Email");
// Enter field description to appear in the dialog box
var fieldDescription = Array("Title", "Name", "Company", "Email");
// 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;
}
}
However, my form should close and redirect when submitted. Am I able to have both these actions when the submit button is clicked, as well as validating the fields?
Any help would be gratefully appreciated as I'm a total javascript dummy.
Many thanks
Vicky
I am not sure that I understand your problem. Normally a redirect for a form is done via action="url" of the form tag like so:
<form name="reg" method="post" action="reply.htm" onsubmit="return validate();">
function validate_reg()
{
valid = true;
if (( document.reg.email.value == "required" ) ¦ ( document.reg.email.value == "" ))
{
alert("Please fill in the 'Email' box.");
document.reg.email.focus();
valid = false;
}
if ( document.reg.password.value == "" )
{
alert("Please fill in the 'Password' box.");
document.reg.password.focus();
valid = false;
}
return valid;
}
Sincerely