Forum Moderators: open

Message Too Old, No Replies

onSubmit question

Validating a form AND redirecting it

         

vicksm

1:16 pm on Jun 28, 2005 (gmt 0)

10+ Year Member



Hi

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

wasproject

12:28 pm on Jul 1, 2005 (gmt 0)

10+ Year Member



Hi 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();">

I am not a javascript expert and allthough your "formCheck" seems to be able and intended especially to deal with extensive forms containing many fields, I wonder whether it is not a bit too elaborate and complicated for 4 fields only. Here is an example of a form validation that I wrote for a simple registration form for you too compare with.

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;
}

I hope the above will be of some use to you.

Sincerely

vicksm

12:32 pm on Jul 1, 2005 (gmt 0)

10+ Year Member



Thank you wasproject. I really appreciate your response. I actually found a Dreamweaver extension which sorted out my problem.

Thank you anyway.

Vicky