2014-10-06 126 views
0

我有一個input選擇二標籤:輸入添加電子郵件格式的標籤後,按鍵「輸入」,「空間」,「標籤」

 <label id="email2_error">Email is not in the correct format</label> 

    <input type="text" style="width:500px" id="emailTags" placeholder="add email" /> 

按鍵之後我打電話的方法addTag

 <script type="text/javascript"> 
    $(document).ready(function() { 
     $("select#emailTags").select2({ 
      placeholder: "[email protected]", 
      allowClear: true 
     }); 

jQuery("#emailTags").keypress(function (event) { 

        //See if the key pressed is 'space' 
        if (event.which == 32) { 
         addTag(); 
        } 
        //See if the key pressed is 'enter' 
        if (event.which == 13) { 
         addTag(); 
        } 
        //See if the key pressed is 'tab' 
        if (event.which == 8) { 
         addTag(); 
        } 

       }); 

    }); 



      function addTag() { 

       var email = $('#emailTags').val() 
       if (!validateEmail(email)) { 
        $("label#email2_error").show(); 

        return false; 
       } else { 

        $("label#email2_error").hide(); 
        //Adding the tag 
       $('#emailTags').select2('val', $('#emailTags').val()); 
       } 
function validateEmail(email) { 

       var re = /^(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; 
       return re.test(email); 
      } 
     </script> 

的標籤沒有添加,有人建議?

+0

你不是應該把報價裏面的正則表達式? – 2014-10-06 16:31:03

+0

@ISuthanBala方法validateEmail做工精細,在控制檯我測試,它工作正常 – Alex 2014-10-06 16:39:57

回答

0

這裏是一個解決方案只需要更改腳本

if (self.isOpen()) { 
    if (key === KEYS.ENTER) { 
     self.trigger('results:select'); 

     evt.preventDefault(); 
    } else if ((key === KEYS.SPACE && evt.ctrlKey)) { 
     self.trigger('results:toggle'); 

     evt.preventDefault(); 
    } else if (key === KEYS.UP) { 
     self.trigger('results:previous'); 

     evt.preventDefault(); 
    } else if (key === KEYS.DOWN) { 
     self.trigger('results:next'); 

     evt.preventDefault(); 
    } else if (key === KEYS.ESC || key === KEYS.TAB) { 
     self.close(); 

     evt.preventDefault(); 
    } 
    } else { 
    if (key === KEYS.ENTER || key === KEYS.SPACE || 
     ((key === KEYS.DOWN || key === KEYS.UP) && evt.altKey)) { 
     self.open(); 

     evt.preventDefault(); 
    } 
    } 

更換

if (self.isOpen()) { 
    if (key === KEYS.ENTER || key === KEYS.TAB) { 
     self.trigger('results:select'); 

     evt.preventDefault(); 
    } else if ((key === KEYS.SPACE && evt.ctrlKey)) { 
     self.trigger('results:toggle'); 

     evt.preventDefault(); 
    } else if (key === KEYS.UP) { 
     self.trigger('results:previous'); 

     evt.preventDefault(); 
    } else if (key === KEYS.DOWN) { 
     self.trigger('results:next'); 

     evt.preventDefault(); 
    } else if (key === KEYS.ESC || key === KEYS.TAB) { 
     self.close(); 

     evt.preventDefault(); 
    } 
    } else { 
    if (key === KEYS.ENTER || key === KEYS.TAB || key === KEYS.SPACE || 
     ((key === KEYS.DOWN || key === KEYS.UP) && evt.altKey)) { 
     self.open(); 

     evt.preventDefault(); 
    } 
    }