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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compare multiple form elements for duplicate info

Status
Not open for further replies.

ibjdt

Programmer
Nov 25, 2002
63
i have a form with 5 document attachment fields (upload1, upload2...upload5).

onSubmit i would like to compare the value of all 5 fields and alert the user if they have selected the same file 2+ times (as defined by the form field contents being identical in multiple fields).

a min of 1 upload is required, so there could be as many as 4 blank attachment fields. the js comparison would have to disregard blank elements - which would show as identical and give an error.

thanks.
 
what do you have so far? this question reads like a requirements document. we're here to help you in finding problems with your code -- not to write code for you.

have you tried anything so far? if so, what have you got? we'd be glad to help.



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
i started the obvious manual loop approach:

Code:
	if (upload1 == upload2 || upload3 || upload4 || upload5)
	{
		missinginfo += "\n     -  Duplicate Attachments Selected";
		duplicates++;
	}
	if ((upload2 == upload3 || upload4 || upload5) && (!duplicates) && (upload1 != ""))
	{
		missinginfo += "\n     -  Duplicate Attachments Selected";
		duplicates++;
	}......

but this gets old and long in a hurry.

i need some sort of loop that loops through 'upload' element(?) 1-5 and compare the values (if it's not blank).

thanks.
 
You can do it like this.
[tt]
function checkdupfile(obj) { //obj: a form element
var celem=obj.elements;
var cfile=new Array();
var cfname=new Array();
for (var i=0;i<celem.length;i++) {
if (celem.type=="file") {
if (celem.value.replace(/\s/g,"").length!=0) {
cfile.push (celem);
cfname.push (escape(celem.value));
}
}
}

alert("Number of files you have chosen to upload : " + cfile.length);
//or do something if the number is lower than some number

var sbase=cfname.join(" ");
var bdup=false;
for (var i=cfile.length-1;i>-1;i--) {
var rx=new RegExp("\\b"+cfname+"\\b","gi");
var cm=sbase.match(rx);
if (cm && cm.length>1) {
cfile.focus();
bdup=true;
break;
}
}
if (bdup) {
alert("duplicated filess are found");
//or do something
} else {
alert("no duplicated file is found");
//or do something
}
}
[/tt]
 
thanks for the help, but i cannot get the script to work,
have you tried it??

in the form tag i have


onSubmit="checkdupfile();"


is this correct??

thanks.
 
>in the form tag i have
>onSubmit="checkdupfile();"

If it is in the form tag, it is this.

[tt]onsubmit="return checkdupfile(this)"[/tt]

_and_ you add some "return false" provision inside the function. Under what condition depends on your concrete needs. I cannot say for the minimum number because I don't know the number, but I can say for the existing of duplicate file. Like this. If I assume the minimum number be one (1), then change the variable setting.
[tt]
function checkdupfile(obj) { //obj: a form element
[green]var minnum=1;[/green] //change to your need
var celem=obj.elements;
var cfile=new Array();
var cfname=new Array();
for (var i=0;i<celem.length;i++) {
if (celem.type=="file") {
if (celem.value.replace(/\s/g,"").length!=0) {
cfile.push (celem);
cfname.push (escape(celem.value));
}
}
}

alert("Number of files you have chosen to upload : " + cfile.length);
//or do something if the number is lower than some number
[blue]if (cfile.length<minnum) {
return false;
}[/blue]

var sbase=cfname.join(" ");
var bdup=false;
for (var i=cfile.length-1;i>-1;i--) {
var rx=new RegExp("\\b"+cfname+"\\b","gi");
var cm=sbase.match(rx);
if (cm && cm.length>1) {
cfile.focus();
bdup=true;
break;
}
}
if (bdup) {
alert("duplicated files are found");
//or do something
[blue]return false;[/blue]
} else {
alert("no duplicated file is found");
//or do something
[blue]return true;[/blue]
}
}
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top