【问题标题】:JavaScript email validation contains specified addressJavaScript 电子邮件验证包含指定地址
【发布时间】:2016-06-02 12:07:28
【问题描述】:

我正在寻找电子邮件表单字段上的 javascript 验证。在提交时,我想验证电子邮件是否包含@specifieddomain.com,然后提交其他错误消息“请使用您的公司电子邮件”

<div id="openModal" class="modalDialog">
    <div>
        <a href="#close" title="Close" class="close">X</a>

     <p><form id="microsubs_form" method="post" action="/" class="" >

        <input type="text" id="ms_firstName" required="true" placeholder="First Name" style="margin-bottom:20px;">
        <input type="text" id="ms_lastName" required="true" style="float:right; alignment-adjust:central; clear:right" placeholder="Last Name" style="margin-bottom:20px;">
        <input type="email" id="ms_email" required="true" style="float:left;" placeholder="Corporate Email address">
        <input type="number" id="ms_telephoneNumber" required="true" style="float:right; alignment-adjust:central; clear:right">


        </form></p>

        <p></p>

    </div>
</div>

谢谢

【问题讨论】:

  • HTML:&lt;input pattern="\\w+@specifieddomain\\.com" JS:emailValue.endsWith(@specifieddomain.com);
  • 这应该不是一个困难的网络搜索,您应该在此处提问之前进行基础研究
  • @charlietfl 是的,这是一个简单的网络搜索,但我是一名软件工程师而不是开发人员,并且有一个解决方案,只是看看那里最好的,因为有许多不同的实现。
  • 那么正确的做法是展示你已经拥有的东西......不要像没有研究过一样问。

标签: javascript jquery html


【解决方案1】:

1) 在 HTML 中

像这样更改输入电子邮件:

<input type="email" pattern="\w+@specifieddomain\.com" style="float:left;" placeholder="Corporate Email address">

最终代码:

<html>
<head>
</head>
<body>
   <div id="openModal" class="modalDialog">
    <div>
        <a href="#close" title="Close" class="close">X</a>

     <p><form id="microsubs_form" method="post" action="/" class="" >

        <input type="text" id="ms_firstName" required="true" placeholder="First Name" style="margin-bottom:20px;">
        <input type="text" id="ms_lastName" required="true" style="float:right; alignment-adjust:central; clear:right" placeholder="Last Name" style="margin-bottom:20px;">
        <input type="email" pattern="\w+@specifieddomain\.com" style="float:left;" placeholder="Corporate Email address">
        <input type="number" id="ms_telephoneNumber" required="true" style="float:right; alignment-adjust:central; clear:right">
        <input type="submit">
        </form>

        <p></p>

    </div>
</div>
</body>
</html>

2) 在 JavaScript 中

像这样改变表格

<form id="microsubs_form" method="post" action="/" class="" onsubmit="return validEmail()" >

并使用测试方法,

<script>
           var emil = document.getElementById("email");
           var patt = /\w+@specifieddomain\.com/;
           function validEmail() {
               if (!patt.test(emil.value)) {
                   alert("please use your company email");
                   return false;
               }
               else
                   return true;
           }
       </script>

最终代码:

<html>
<head>
</head>
<body>
   <div id="openModal" class="modalDialog">
    <div>
        <a href="#close" title="Close" class="close">X</a>

     <p><form id="microsubs_form" method="post" action="/" class="" onsubmit="return validEmail()" >

        <input type="text" id="ms_firstName" required="true" placeholder="First Name" style="margin-bottom:20px;">
        <input type="text" id="ms_lastName" required="true" style="float:right; alignment-adjust:central; clear:right" placeholder="Last Name" style="margin-bottom:20px;">
        <input id="email" style="float:left;" placeholder="Corporate Email address">
        <input type="number" id="ms_telephoneNumber" required="true" style="float:right; alignment-adjust:central; clear:right">
        <input type="submit">
        </form>
        <p></p>
    </div>
       <script>
           var emil = document.getElementById("email");
           var patt = /\w+@specifieddomain\.com/;
           function validEmail() {
               if (!patt.test(emil.value)) {
                   alert("please use your company email");
                   return false;
               }
               else
                   return true;
           }
       </script>
</div>
</body>
</html>

【讨论】:

  • 很棒的解决方案,类似于我的测试版本。感谢大家的帮助。
【解决方案2】:
<script type="text/javascript">
    function emailvalidator(){
          var email = document.getElementById('ms_email').value;

          if(email.search("@yourdomain.com")!=-1){
                alert("wrong email"); 
          }else{
                alert("email is valid!");
          }
   }
</script>

在你的文件中添加这个函数,然后在提交时调用这个函数。

【讨论】:

    【解决方案3】:

    试试这个..

    $('#microsubs_form').on('submit',function(){
    
        var email = $('#ms_email').val();
        var atpos = email.indexOf("@");
        var dotpos = email.lastIndexOf(".");
        if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length) {
            alert("Not a valid e-mail address");
            return false;
        }
    
    })
    

    【讨论】:

      猜你喜欢
      • 2018-03-13
      • 2011-08-22
      • 2014-06-12
      • 1970-01-01
      • 2011-09-02
      • 2013-02-20
      • 2014-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多