Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New CF user with multiple forms validation problems

Status
Not open for further replies.

Newbedude

Programmer
Jun 6, 2003
9
0
0
US
Thank you in advance for the help.
I have 2 separate forms on one main page
one is for Adding the other is to update a list of employees. The reason i did this was to validate the information client side using the Yaromat add on behavior each of them have their own submit buttons.
Question #1
The JS validation only works with the add on submit button and does not work with the update? Any help?

Question #2
When validating and updating server side im having trouble with the action page. I can not seem to pass the form values for that specific form. HOw do you pass/call a specific form? ex with 1 form it would be something like Form.addID but with multiple forms i can not seem to pass it properly for validation.

I new to Cold fusion so I apologize if this is a too complex of a question and the length of it But I appricate any guidence you can give me. Thank you for your time and consideration!!!


<html>
<head>

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--- This is the JS Yaromat automatically creates--->
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf(&quot;?&quot;))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers.document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function YY_checkform() { //v4.66
//copyright (c)1998,2002 Yaromat.com
var args = YY_checkform.arguments; var myDot=true; var myV=''; var myErr='';var addErr=false;var myReq;
for (var i=1; i<args.length;i=i+4){
if (args[i+1].charAt(0)=='#'){myReq=true; args[i+1]=args[i+1].substring(1);}else{myReq=false}
var myObj = MM_findObj(args.replace(/\[\d+\]/ig,&quot;&quot;));
myV=myObj.value;
if (myObj.type=='text'||myObj.type=='password'||myObj.type=='hidden'){
if (myReq&&myObj.value.length==0){addErr=true}
if ((myV.length>0)&&(args[i+2]==1)){ //fromto
var myMa=args[i+1].split('_');if(isNaN(myV)||myV<myMa[0]/1||myV > myMa[1]/1){addErr=true}
} else if ((myV.length>0)&&(args[i+2]==2)){
var rx=new RegExp(&quot;^[\\w\.=-]+@[\\w\\.-]+\\.[a-z]{2,4}$&quot;);if(!rx.test(myV))addErr=true;
} else if ((myV.length>0)&&(args[i+2]==3)){ // date
var myMa=args[i+1].split(&quot;#&quot;); var myAt=myV.match(myMa[0]);
if(myAt){
var myD=(myAt[myMa[1]])?myAt[myMa[1]]:1; var myM=myAt[myMa[2]]-1; var myY=myAt[myMa[3]];
var myDate=new Date(myY,myM,myD);
if(myDate.getFullYear()!=myY||myDate.getDate()!=myD||myDate.getMonth()!=myM){addErr=true};
}else{addErr=true}
} else if ((myV.length>0)&&(args[i+2]==4)){ // time
var myMa=args[i+1].split(&quot;#&quot;); var myAt=myV.match(myMa[0]);if(!myAt){addErr=true}
} else if (myV.length>0&&args[i+2]==5){ // check this 2
var myObj1 = MM_findObj(args[i+1].replace(/\[\d+\]/ig,&quot;&quot;));
if(myObj1.length)myObj1=myObj1[args[i+1].replace(/(.*\[)|(\].*)/ig,&quot;&quot;)];
if(!myObj1.checked){addErr=true}
} else if (myV.length>0&&args[i+2]==6){ // the same
var myObj1 = MM_findObj(args[i+1]);
if(myV!=myObj1.value){addErr=true}
}
} else
if (!myObj.type&&myObj.length>0&&myObj[0].type=='radio'){
var myTest = args.match(/(.*)\[(\d+)\].*/i);
var myObj1=(myObj.length>1)?myObj[myTest[2]]:myObj;
if (args[i+2]==1&&myObj1&&myObj1.checked&&MM_findObj(args[i+1]).value.length/1==0){addErr=true}
if (args[i+2]==2){
var myDot=false;
for(var j=0;j<myObj.length;j++){myDot=myDot||myObj[j].checked}
if(!myDot){myErr+='* ' +args[i+3]+'\n'}
}
} else if (myObj.type=='checkbox'){
if(args[i+2]==1&&myObj.checked==false){addErr=true}
if(args[i+2]==2&&myObj.checked&&MM_findObj(args[i+1]).value.length/1==0){addErr=true}
} else if (myObj.type=='select-one'||myObj.type=='select-multiple'){
if(args[i+2]==1&&myObj.selectedIndex/1==0){addErr=true}
}else if (myObj.type=='textarea'){
if(myV.length<args[i+1]){addErr=true}
}
if (addErr){myErr+='* '+args[i+3]+'\n'; addErr=false}
}
if (myErr!=''){alert('The required information is incomplete or contains errors:\t\t\t\t\t\n\n'+myErr)}
document.MM_returnValue = (myErr=='');
}
//-->
</script>

</head>
<body onLoad=&quot;clock()&quot;>
<cfset EditMode=IsDefined(&quot;url.ID&quot;)>
<cfif EditMode>
<cfquery datasource=&quot;Table&quot; name=&quot;Employees_edit&quot;>
Select *
From Process
Where ID=#url.ID#
</cfquery>
</cfif>

<cfquery datasource=&quot;table&quot; name=&quot;Employees&quot;>
Select *
From Process
Order By ID
</cfquery>

<h1 align=&quot;center&quot;>Employee List</h1>
<table align=&quot;center&quot; width=&quot;75%&quot; border=&quot;1&quot;>
<tr bgcolor=&quot;#CCCCCC&quot;>
<td width=&quot;18%&quot;><div align=&quot;center&quot;><font color=&quot;#0000FF&quot;>ID</font></div></td>
<td width=&quot;51%&quot;><div align=&quot;center&quot;><font color=&quot;#0000FF&quot;>Employee</font></div></td>
<td width=&quot;15%&quot;><div align=&quot;center&quot;><font color=&quot;#0000FF&quot;>Edit</font></div></td>
<td width=&quot;16%&quot;><div align=&quot;center&quot;><font color=&quot;#0000FF&quot;>Delete</font></div></td>
</tr>
<cfoutput query=&quot;Employees&quot;>

<cfif CurrentRow MOD 2 is 1>
<cfset bgcolor=&quot;lime&quot;>
<cfelse>
<cfset bgcolor=&quot;white&quot;>
</cfif>

<cfif EditMode>
<cfif #ID# eq #URL.ID#>

<tr bgcolor=&quot;#bgcolor#&quot;>

<form action=&quot;action.cfm&quot; method=&quot;post&quot; name=&quot;myform2&quot; onSubmit=&quot;YY_checkform('myform2','editID','##q','0','Field \'editID\' is not valid.','editname','##q','0','Field \'editname\' is not valid.');return document.MM_returnValue&quot;&quot;>
<td>
<input name=&quot;editID&quot; type=&quot;text&quot; value=&quot;#ID#&quot; size=&quot;10&quot;
MaxLength=&quot;10&quot;
Required=&quot;yes&quot;> </td>
<td>
<input name=&quot;editname&quot; type=&quot;text&quot; value=&quot;#name#&quot;
size=&quot;30&quot;
MaxLength=&quot;30&quot; equired=&quot;yes&quot;> </td>
<td div align=&quot;center&quot;>
<input name=&quot;update&quot; type=&quot;submit&quot; value=&quot;update&quot;>
</div></td>
<td div align=&quot;center&quot;>
<input name=&quot;Cancel&quot; type=&quot;reset&quot; id=&quot;Cancel&quot; value=&quot;Cancel&quot;>
</div></td>
</tr>
</form>
<cfelse>
<tr bgcolor=&quot;#bgcolor#&quot;>
<td><div align=&quot;center&quot;><b>#ID#</b></div></td>
<td><div align=&quot;left&quot;><b>#name#</b></div></td>
<td><div align=&quot;center&quot;><a href=&quot;employeecurrent2a.cfm?ID=#urlEncodedFormat(Trim(ID))#&quot;>Edit</a></div></td>
<td><div align=&quot;center&quot;><a href=&quot;employee_delete.cfm?ID=#ID#&quot;>Delete</a></div></td>
</tr>
</cfif>
<cfelse>
<tr bgcolor=&quot;#bgcolor#&quot;>
<td><div align=&quot;center&quot;><b>#ID#</b></div></td>
<td><div align=&quot;left&quot;><b>#name#</b></div></td>
<td><div align=&quot;center&quot;><a href=&quot;employeev2a.cfm?ID=#urlEncodedFormat(Trim(ID))#&quot;>Edit</a></div></td>
<td><div align=&quot;center&quot;><a href=&quot;employee_delete.cfm?ID=#ID#&quot;>Delete</a></div></td>
</tr>
</cfif>
</cfoutput>
<cfoutput>
<cfif not EditMode>
<tr>
<td>
<form action=&quot;action.cfm&quot; name=&quot;myform&quot; onSubmit=&quot;YY_checkform('myform','addID','##1_100','1','Field \'addID\' is not valid.','addName','##q','0','Field \'addName\' is not valid.');return document.MM_returnValue&quot;>
<input name=&quot;addID&quot; type=&quot;text&quot;
MaxLength=&quot;10&quot;
Required=&quot;yes&quot;
Message=&quot;ID is Required!&quot;
Validate=&quot;integer&quot;> </td>
<td> <input name=&quot;addName&quot; type=&quot;text&quot;
size=&quot;30&quot;
MaxLength=&quot;30&quot;>
</td>
<td colspan=&quot;2&quot;> <div align=&quot;center&quot;>
<input name=&quot;Add&quot; type=&quot;submit&quot; value=&quot;Add&quot; >
</form>
</div></td>
</tr>
</cfif></cfoutput>
</body>
</html>



<!---Action.cfm--->

<cfquery datasource=&quot;table&quot; name=&quot;check_record&quot;>
Select ID
From Process
Where ID=#Form.addID#
</cfquery>

<cfif ISDefined(&quot;Form.update&quot;)>
<Cfif Len(Trim(editname)) is 0>
<h2> Error! Name can not be Blank!</h2>
<cfelse>
<cfquery datasource=&quot;table&quot;>
Update Process
Set name='#Trim(Form.editname)#'
Where id=#Form.editID#
</cfquery>
<cflocation url=&quot;Employee---.cfm&quot;></cfif>
</cfif>

<cfif ISDefined(&quot;Form.Add&quot;)>
<cfif Len(Trim(addname)) eq 0>
<h2> Error! Name can not be Blank!</h2>
<cfelseif len(trim(addID)) eq 0>
<h2>Error! ID can not be Blank!</h2>
<cfabort>
<cfelseif IsNumeric(addID) is &quot;no&quot;>
<h2> Error! ID must be numeric!</h2>
<cfabort>
<cfelseif check_record.RecordCount eq '0'>
<cfquery datasource=&quot;table&quot; name=&quot;insert_record&quot;>
Insert into Process (ID, name)
Values ('#Trim(Form.ID)#',
'#Trim(Form.name)#')
</cfquery>
<cflocation url=&quot;Employee---.cfm&quot;>
<cfelse>
This Record already exists!
</cfif>
<cfelse>
Wrong

</cfif>

<cfif ISDefined(&quot;Form.Cancel&quot;)>
what
<cfelse>
fsf<cflocation url=&quot;Employee---.cfm&quot;>
</cfif>
 
When i had to evaluate the form field in the past , i used
(document.form_name.Field_name.checked == true) in the javascript function for a checkbox for example. For a radio button: document.form_name.Field_name.value
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top