<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
A组<br />
<input id="a1" name="a1" size="3" type="text" class="input" >
<input id="a2" name="a2" size="3" type="text" class="input" >
<input id="a3" name="a3" size="3" type="text" class="input" >
<input id="a4" name="a4" size="3" type="text" class="input" >
<input id="a5" name="a5" size="3" type="text" class="input" >
<input id="a6" name="a6" size="3" type="text" class="input" >
<input id="a7" name="a7" size="3" type="text" class="input" >
<br />
<button onclick="check('a',1,7)">检查A组</button>
<br /><br />
B组<br />
<input id="b1" name="b1" size="3" type="text" class="input" >
<input id="b2" name="b2" size="3" type="text" class="input" >
<input id="b3" name="b3" size="3" type="text" class="input" >
<input id="b4" name="b4" size="3" type="text" class="input" >
<br />
<button onclick="check('b',1,4)">检查B组</button>
<br /><br />
C组<br />
<input id="c1" name="c1" size="3" type="text" class="input" >
<input id="c2" name="c2" size="3" type="text" class="input" >
<input id="c3" name="c3" size="3" type="text" class="input" >
<input id="c4" name="c4" size="3" type="text" class="input" >
<input id="c5" name="c5" size="3" type="text" class="input" >
<br />
<button onclick="check('c',1,5)">检查C组</button>
<br />
<script type="text/javascript">
function check(id,from,to)
{
var data = {};
for(var i=from;i<=to;i++)
{
var value = document.getElementById(id+i).value;
if(value in data)
{
alert("存在重复");
data = null;
return true;
}
data[value]=0;
}
alert("没有重复");
return false;
}
</script>
</body>
</html>
<!--
function mm(a)
{
return /(\x0f[^\x0f]+)\x0f[\s\S]*\1/.test("\x0f"+ a.join("\x0f\x0f") +"\x0f");
} function showin(){
var arya = new Array();
arya[0]=document.form1.a1.value;
arya[1]=document.form1.a2.value;
arya[2]=document.form1.a3.value;
arya[3]=document.form1.a4.value;
arya[4]=document.form1.a5.value;
arya[5]=document.form1.a6.value;
arya[6]=document.form1.a7.value;
if(mm(arya)) {
alert('A组有重复的');
return false
}
var aryb = new Array();
aryb[0]=document.form1.b1.value;
aryb[1]=document.form1.b2.value;
aryb[2]=document.form1.b3.value;
aryb[3]=document.form1.b4.value;
if(mm(aryb)) {
alert('B组有重复的');
return false
}
var aryc = new Array();
aryc[0]=document.form1.c1.value;
aryc[1]=document.form1.c2.value;
aryc[2]=document.form1.c3.value;
aryc[3]=document.form1.c4.value;
aryc[4]=document.form1.c5.value;
if(mm(aryc)) {
alert('C组有重复的');
return false
}
}
//-->
</SCRIPT>
<form action="" method="post" name="form1" onsubmit="return showin()">
<input name="a1" size="3" type="text" class="input" >
<input name="a2" size="3" type="text" class="input" >
<input name="a3" size="3" type="text" class="input" >
<input name="a4" size="3" type="text" class="input" >
<input name="a5" size="3" type="text" class="input" >
<input name="a6" size="3" type="text" class="input" >
<input name="a7" size="3" type="text" class="input" > B组如
<input name="b1" size="3" type="text" class="input" >
<input name="b2" size="3" type="text" class="input" >
<input name="b3" size="3" type="text" class="input" >
<input name="b4" size="3" type="text" class="input" >
C组如
<input name="c1" size="3" type="text" class="input" >
<input name="c2" size="3" type="text" class="input" >
<input name="c3" size="3" type="text" class="input" >
<input name="c4" size="3" type="text" class="input" >
<input name="c5" size="3" type="text" class="input" >
<input type="submit" class="button" value="提交">
</form>
1 取得A组的所有元素值
定义一个数组(var a = [])和一个字符串(var str = ""),数组a分别装A组取得的数值,字符串存储A组所有的数值,每个数值中间用个特别符号隔开,如*,$...
最后,数组a样式为a[0] = **, a[1] = **, ... a[6] = **;
字符串str样式为str = **$**$...$**;2 检测A组中是否存在相同值
数组a进行for循环,对字符串进行正则匹配,出现匹配的情况,则需要对匹配位置的前一个字符和后一个字符进行判断,如果这两个字符均为分隔符字符,如上的$,则表示匹配,中断for循环,返回true;否则进行下一个数值的比较注:
如果用两次for循环也可以实现,不知道效率会有影响不以上仅供参考...
function mm(a)
{
return /(\x0f[^\x0f]+)\x0f[\s\S]*\1/.test("\x0f"+ a.join("\x0f\x0f") +"\x0f");
}
学习了,以上的功能。
<!--
function RepeatChk(){
Array.prototype.chkRepeat=function(){
var tArr=[],tArr1=[],j=0;
for(var i=this.length;i>0;i--){
if(!tArr[this[i-1]]){tArr[this[i-1]]=1; }
else{
if(tArr[this[i-1]]++==1) tArr1[j++]=this[i-1];
this.splice(i-1,1);
}
}
return tArr1;
} chk(document.getElementsByName("a"),"A组");
chk(document.getElementsByName("b"),"B组")
chk(document.getElementsByName("c"),"C组")
}function chk(obj,groupname){
var arr=[];
for(var i=0;i<obj.length;i++){arr[i]=obj[i].value;}
alert(groupname+'提交的数据为:'+arr);
alert("含重复项:"+arr.chkRepeat());
alert("剔除重复后数据:"+arr);
}
//-->
</script>A组如
<input name="a" size="3" type="text" class="input" >
<input name="a" size="3" type="text" class="input" >
<input name="a" size="3" type="text" class="input" >
<input name="a" size="3" type="text" class="input" >
<input name="a" size="3" type="text" class="input" >
<input name="a" size="3" type="text" class="input" >
<input name="a" size="3" type="text" class="input" >
<br>
B组如
<input name="b" size="3" type="text" class="input" >
<input name="b" size="3" type="text" class="input" >
<input name="b" size="3" type="text" class="input" >
<input name="b" size="3" type="text" class="input" >
<br>
C组如
<input name="c" size="3" type="text" class="input" >
<input name="c" size="3" type="text" class="input" >
<input name="c" size="3" type="text" class="input" >
<input name="c" size="3" type="text" class="input" >
<input name="c" size="3" type="text" class="input" >
<br>
<input type="submit" class="button" value="提交" onclick="return RepeatChk()">
return /(\x0f\x0f[^\x0f]+)\x0f[\s\S]*\1/.test("\x0f\x0f"+ a.join("\x0f\x0f") +"\x0f\x0f"); 应该是没问题了吧你多测试几个数据如果还不可以再告诉我
输入:p,a,b,ab,c,d,e,提示"A组有重复的"
用循环吧
给你一个可以用的
<SCRIPT LANGUAGE="JavaScript">
<!--
function mm(obj,n)
{ for (var i = 0, len = obj.length; i <len; i++) { n(obj[i], i);}
//return /(\x0f\x0f[^\x0f]+)\x0f[\s\S]*\1/.test("\x0f\x0f"+ a.join("\x0f\x0f") +"\x0f\x0f");
} function showin(){
var arya = new Array();
arya[0]=document.form1.a1.value;
arya[1]=document.form1.a2.value;
arya[2]=document.form1.a3.value;
arya[3]=document.form1.a4.value;
arya[4]=document.form1.a5.value;
arya[5]=document.form1.a6.value;
arya[6]=document.form1.a7.value;
var t ;
var flag=false;
mm(arya.sort(), function(o,i){
if(i>0 && o == t){
flag=true;
}
t = o;
})
if(flag){alert('A组有重复的');return false;}
var aryb = new Array();
aryb[0]=document.form1.b1.value;
aryb[1]=document.form1.b2.value;
aryb[2]=document.form1.b3.value;
aryb[3]=document.form1.b4.value;
mm(aryb.sort(), function(o,i){
if(i>0 && o == t){
flag=true;}
t = o;
})
if(flag){alert('B组有重复的');return false}
var aryc = new Array();
aryc[0]=document.form1.c1.value;
aryc[1]=document.form1.c2.value;
aryc[2]=document.form1.c3.value;
aryc[3]=document.form1.c4.value;
aryc[4]=document.form1.c5.value;
mm(aryc.sort(), function(o,i){
if(i>0 && o == t){
flag=true;}
t = o;
})
if(flag){alert('C组有重复的');return false}
}
//-->
</SCRIPT>
<form action="" method="post" name="form1" onsubmit="return showin()">
<input name="a1" size="3" type="text" class="input" >
<input name="a2" size="3" type="text" class="input" >
<input name="a3" size="3" type="text" class="input" >
<input name="a4" size="3" type="text" class="input" >
<input name="a5" size="3" type="text" class="input" >
<input name="a6" size="3" type="text" class="input" >
<input name="a7" size="3" type="text" class="input" > B组如
<input name="b1" size="3" type="text" class="input" >
<input name="b2" size="3" type="text" class="input" >
<input name="b3" size="3" type="text" class="input" >
<input name="b4" size="3" type="text" class="input" >
C组如
<input name="c1" size="3" type="text" class="input" >
<input name="c2" size="3" type="text" class="input" >
<input name="c3" size="3" type="text" class="input" >
<input name="c4" size="3" type="text" class="input" >
<input name="c5" size="3" type="text" class="input" >
<input type="submit" class="button" value="提交">
</form>
虽然使用正则表达式代码更简练,更COOL,
但执行效率应该要差一些,
在正则表达式中进行进行匹配的时候通常要进行更多的循环和比较。另外,就这个问题来说,
不使用正则表达式显然代码更容易理解,
更容易维护。chinmo正则表达式运用真的很强。
function ckRepeat(arr){
var str=arr.join('\n');
if(/\n{2,}/.test(str))return null;//这句检查是否允许又空值的,如果不检查空值,可以去掉这句,并且ckForm函数对应的代码可以去掉
return /(^|\n)([^\n]+)(\n[^\n]*)*?(\n\2)(?=(\n|$))/g.test(str);//=========
}function ckForm(f){
var r=ckRepeat(new Array(f.a1.value,f.a2.value,f.a3.value,f.a4.value,f.a5.value,f.a6.value,f.a7.value));
if(r==null){
alert('A组有未填写的项!');return false;
}
else if(r){
alert('A组有重复!');return false;
}
r=ckRepeat(new Array(f.b1.value,f.b2.value,f.b3.value,f.b4.value))
if(r==null){
alert('B组有未填写的项!');return false;
}
else if(r){
alert('B组有重复!');return false;
}
r=ckRepeat(new Array(f.c1.value,f.c2.value,f.c3.value,f.c4.value,f.c5.value));
if(r==null){
alert('AC组有未填写的项!');return false;
}
else if(r){
alert('C组有重复!');return false;
}
}
</script>
<form action="" method="post" name="form1" onsubmit="return ckForm(this)">
A组<br />
<input name="a1" size="3" type="text" class="input" >
<input name="a2" size="3" type="text" class="input" >
<input name="a3" size="3" type="text" class="input" >
<input name="a4" size="3" type="text" class="input" >
<input name="a5" size="3" type="text" class="input" >
<input name="a6" size="3" type="text" class="input" >
<input name="a7" size="3" type="text" class="input" >
<br />B组 <br />
<input name="b1" size="3" type="text" class="input" >
<input name="b2" size="3" type="text" class="input" >
<input name="b3" size="3" type="text" class="input" >
<input name="b4" size="3" type="text" class="input" >
<br />C组<br />
<input name="c1" size="3" type="text" class="input" >
<input name="c2" size="3" type="text" class="input" >
<input name="c3" size="3" type="text" class="input" >
<input name="c4" size="3" type="text" class="input" >
<input name="c5" size="3" type="text" class="input" >
<br /><input type="submit" class="button" value="提交">
</form>
^)^chinmo的正则少了一个关键的“?”。下面的测试通过毫无问题。
(\x0f[^\x0f]+\x0f)[\s\S]*?\1<script>
function distinct(a){
document.write(a)
return /(\x0f[^\x0f]+\x0f)[\s\S]*?\1/g.test(a)
}
var ary = new Array("01","02","23","3","356","5","6");
//01,02,23,3,356,5,6
//3,33,333,3333,2,1,4
//"p","a","b","ab","c","d","e"
a=distinct("\x0f"+ary.join("\x0f\x0f") +"\x0f")
alert(a)
</script>如果要去除重复可以参考这个帖子
http://topic.csdn.net/u/20080317/10/9d1cce71-3252-40d1-a4a9-e5125c83d90c.html
<html>
<title>MultiVerification</title>
<script language="javascript">
</script>
<body>
<input type="text" name="txt" value="3333"><br/>
<input type="text" name="txt" value="33"><br/>
<input type="text" name="txt" value="333"><br/>
<input type="text" name="txt" value="3"><br/>
<input type="text" name="txt" value="abc"><br/>
<input type="text" name="txt" value="a"><br/>
<input type="text" name="txt" value="b"><br/>
<input type="text" name="txt" value="c"><br/>
<input type="button" name="submit" value="Submit" onclick="Verify();">
</body>
</html>
请把下面的JavaScript代码放在上面的HTML代码的SCRIPT中.
function Verify()
{
var txt = document.getElementsByName("txt");
var bln = false;
var val;
var str = "\n";
for(var i=0; l=txt.length, i<l; i++)
{
val = txt[i].value;
if(str.indexOf("\n" + val + "\n") >= 0)
{
bln = true;
break;
}
str += val + "\n";
}
if (bln)
{
alert("Duplicate value!");
}
else
{
alert("No duplicate value!");
}
}
原帖
http://topic.csdn.net/u/20080317/10/9d1cce71-3252-40d1-a4a9-e5125c83d90c.html