2017-09-01 55 views
0

不要允許用戶與免費郵箱addresses.It註冊必須只使用jQuery驗證插件不要讓Gmail,雅虎等使用jQuery電子郵件地址驗證沒有正則表達式

$("form").validate({ 
     rules: { 
      username: { 
       required: true, 
       minlength: 2, 
       nofreeemail: true 
      } 
     }, 
     messages: { 
      username: { 
       required: "Please enter a username", 
       minlength: "Your email must consist of at least 2 characters", 
       nofreeemail: "Please use your business email" 
      } 
     } 
    }); 
+1

究竟有什麼您的問題,你有什麼迄今所做的解決呢? –

+0

[docs](https://jqueryvalidation.org/validate/)顯示你可以添加一個'depend'選項。然後,您可以在電子郵件字段中定義不允許使用的關鍵字數組(Gmail,雅虎等) – Laazo

回答

0

只需添加新方法:

$.validator.addMethod('noemail', function (value) { 
    return /^([\w-.][email protected](?!gmail\.com)(?!yahoo\.com)(?!hotmail\.com)(?!mail\.ru)(?!yandex\.ru)(?!mail\.com)([\w-]+.)+[\w-]{2,4})?$/.test(value); 
}, 'Free email addresses are not allowed.'); 

rules: { 
     username: { 
      required: true, 
      minlength: 2, 
      noemail: true 
     } 
    }, 
相關問題