JavaScript电子邮件验证包含指定的地址

时间:2016-06-02 12:07:29

标签: javascript jquery html

我正在寻找电子邮件表单字段的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>

谢谢

3 个答案:

答案 0 :(得分:1)

HTML中的

1)

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

<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>

答案 1 :(得分:0)

<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>

在您的文件中添加此功能,然后在提交时调用此功能。

答案 2 :(得分:0)

试试这个..

$('#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;
    }

})