close
文章出處

1數組遍歷判斷兩個輸入框之間是否有輸入重復的值!
今天在工作的過程,遇到一個這樣的問題。有兩個輸入框,
用戶可輸入多值,用逗號隔開,例如:1,2,3,4
第二個輸入框也是一樣,但是第一輸入框是必填的,現在就是要取這兩個輸入框逗號隔開的值而且不能重復。
以下為我實現的腳本代碼,歡迎指正。
function checkSend()
{
    
var Accept = document.getElementById("txtAddresseeName").value;
    
if( Accept=="")
    
{
        alert(
"請輸入");
        
return false;
    }

       
if(  CheckAcceptName(Accept,"第一個框") )
    
{   
        
if(document.getElementById("txtOthersName")!=null)
        
{
           
var CopyForName = document.getElementById("txtOthersName").value;  
            
if( CopyForName !="")
            
{
                  
if( CheckAcceptName(CopyForName,"第二個框"))  
                  
{
                          
var ErrorMsg="";    
                           
if(Accept.substring(Accept.length-1,Accept.length)==","
                                 Accept
=Accept.substr(0,   Accept.length-1);   //去掉最后一個逗號 
                           var Accepts = Accept.split(","); //將逗號拆分
                           for(var i=0;i<Accepts.length;i++)
                           
{
                                AcceptName 
= Accepts[i];   
                                
if( AcceptName!="")
                                
{
                                    
var count = 0;
                                     
if(CopyForName.substring(CopyForName.length-1,CopyForName.length)==","
                                            CopyForName
=CopyForName.substr(0,   CopyForName.length-1);   //去掉最后一個逗號 
                                    var CopyForNames = CopyForName.split(","); //將逗號拆分
                                    for(var j=0;j<CopyForNames.length;j++)
                                    
{
                                        
if( AcceptName == CopyForNames[j] )
                                            count 
++ ;  
                                    }

                                    
                                    
if( count > 0 ) //如果重復
                                    {
                                        
if( ErrorMsg.indexOf(AcceptName) == -1//添加錯誤提示
                                             ErrorMsg = ErrorMsg +AcceptName +",";
                                    }

                                }

                           }

                           
                           
if(  ErrorMsg !="")
                           
{
                                alert(
"第一個輸入框和第二個框中"+ErrorMsg+"出現重復。");
                                
return false;
                           }


                   }

             }

        }

    }

       
  
    
return true;
}


//2008.08.07 
//
阿一  檢查兩個輸入框是否出現重復
function CheckAcceptName(AcceptName,type)
{

    
var ErrorMsg="";    
   
if(AcceptName.substring(AcceptName.length-1,AcceptName.length)==","
         AcceptName
=AcceptName.substr(0,   AcceptName.length-1);   //去掉最后一個逗號 
   var Accepts = AcceptName.split(","); //將逗號拆分
   for(var i=0;i<Accepts.length;i++)
   
{
        Name 
= Accepts[i];   
       
if( Name!="")
        
{
            
var count = 0;
            
for(var j=0;j<Accepts.length;j++)
            
{
                
if( Name == Accepts[j] )
                    count 
++ ;  //判斷某值在該數組中是否出現重復
            }

            
            
if( count > 1 ) //如果重復
            {
                
if( ErrorMsg.indexOf(Name) == -1//添加錯誤提示
                     ErrorMsg = ErrorMsg +Name +",";
            }

        }

   }

   
   
if(  ErrorMsg !="")
   
{
        alert(type
+ErrorMsg+"出現重復。");
        
return false;
   }

   
   
return true;
}

不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 AutoPoster 的頭像
    AutoPoster

    互聯網 - 大數據

    AutoPoster 發表在 痞客邦 留言(0) 人氣()