0

I am looking for javascript validation on email form field. On submit i want to validate if email contains @specifieddomain.com then submit else error message "please use your company email"

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

thanks

Yondaime14
  • 113
  • 1
  • 8

3 Answers3

1

1) in HTML

change input email like this :

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

final code :

<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) in javascript

change form like this

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

and use test Method,

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

final code :

<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>
Ehsan
  • 12,655
  • 3
  • 25
  • 44
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>

add this function in your file, then call this function on submit.

CRSSJ
  • 252
  • 5
  • 18
0

Try this..

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

})
Parvez Rahaman
  • 4,269
  • 3
  • 22
  • 39