Click to See Complete Forum and Search --> : help customising form validation script


idiotbear
10-15-2003, 07:59 AM
hi

I've been customising Dreamweaver's form validation script, but there's one thing I still want to do which I can't get to behave properly.

The function which I call looks like this:


function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='', args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
if (val) { nm=val.id; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@' || '.');
if (p<1 || p==(val.length-1)) errors+='- "'+nm+'" must contain a valid e-mail address.\n\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- "'+nm+'" must contain a number.\n\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- "'+nm+'" must contain a number between '+min+' and '+max+'.\n\n';
} } } else if (test.charAt(0) == 'R') errors += '- "'+nm+'" is a required field.\n\n'; }
} if (errors) alert('The information you entered is invalid for \n the following reasons:\n\n'+errors);
document.MM_returnValue = (errors == '');
}


This is called from the onSubmit event like so:

onSubmit=" MM_validateForm('First Name','','R','Surname','','R','E-mail','','RisEmail','Number of Documents','','R'); return document.MM_returnValue; "

Now, what I want to do is to put a confirm box into the onSubmit, asking the user if they're sure they want to proceed before running the MM_validateForm() function. I'd normally do this like:

function confirmProceed()
{
confMsg=Confirm("Are you sure you want to proceed? \n \n You won't be able to edit this information later.")
if (confMsg == true)
{return true}
else
{return false}
}

Then I'd call it in onSubmit like

onSubmit="return confirmProceed"

Now, how can I build that function into the MM_validateForm() function so that you get the confirm first, then if OK is clicked, the MM_validateForm() function fires. If cancel is clicked, the form is not submitted.

I can't get my head round Dreamweaver's code!

Cheers

Rob

pyro
10-15-2003, 08:10 AM
That's because it sucks. It'd be simple enough to write your own validation, and it would pretty much be gaurenteed to work better...

Anway, do it something like this. In your form's onsubmit handler, call the confirm function:

<form action="" onsubmit="confirmProceed();"

Then, code that function like this:

function confirmProceed() {
confMsg=Confirm("Are you sure you want to proceed? \n \n You won't be able to edit this information later.")
if (confMsg == true) {
return MM_validateForm('First Name','','R','Surname','','R','E-mail','','RisEmail','Number of Documents','','R');
}
else {
return false;
}
}Though you'll still have to change DW's validation a bit, as you'll want it to return true or false.

idiotbear
10-15-2003, 08:25 AM
Thanks

how can I get DW's validation to return true or false? Sorry, but I really don't understand DW's sucky script.

I'm an ASP developer and I don't normally do much client-side code - but if DW's javascript is anything like as bad as its server side VB Script, then it MUST suck!

;-)

pyro
10-15-2003, 08:34 AM
Try changing the if (errors) part to read like this:

if (errors) {
alert('The information you entered is invalid for \n the following reasons:\n\n'+errors);
return false;
}
else {
return true;
}

idiotbear
10-15-2003, 08:51 AM
much appreciated - works now :)

pyro
10-15-2003, 08:54 AM
Sure thing... :)