-2

I am making an HTML form with fields validation using JavaScript. I am stuck on email validation. I searched internet and found something like this-

JS Code

function validateemail() {  
    var x=document.myform.email.value;  
    var atposition=x.indexOf("@");  
    var dotposition=x.lastIndexOf(".");  
    if (atposition<1 || dotposition<atposition+2 || dotposition+2>=x.length) {  
        alert("Please enter a valid e-mail address \n atpostion:"+atposition+"\n dotposition:"+dotposition);  
        return false;  
    }  
}  

HTML Code

<body>  

<form name="myform"  method="post" action="#" onsubmit="return validateemail();">  

Email: <input type="text" name="email"><br/>  

<input type="submit" value="register">  
</form>  

Please explain me this?

Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
Anmol Khanotia
  • 123
  • 1
  • 5

3 Answers3

0

Check this i am using something like this i minified some of them You must Enter Valid Email address something like this Example@example.com

$(document).ready(function() {
  $('.insidedivinput').focusout(function() {
    $('.insidedivinput').filter(function() {
      var emil = $('.insidedivinput').val();
      var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
      if (emil.length == 0) {
        $('.fa-check').css('display', 'none');
        $('.fa-close').css('display', 'inline');
        $('.sendmailbuttontrigger').attr('disabled', 'disabled');
        $('.SendEmail').attr('disabled', 'disabled');
      } else if (!emailReg.test(emil)) {
        $('.SendEmail').attr('disabled', 'disabled');
        $('.sendmailbuttontrigger').attr('disabled', 'disabled');
        $('.fa-check').css('display', 'none');
        $('.fa-close').css('display', 'inline');
      } else {
        // alert('Thank you for your valid email');
        $('.fa-close').css('display', 'none');
        $('.sendmailbuttontrigger').removeAttr('disabled');
        $('.fa-check').css('display', 'inline');
      }
    })
  });
});
.fa-check{
  display:none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='email' class='insidedivinput'><i class='fa-check'>Validated</i><i class="fa-close">UnValidated</i>
<button class="sendmailbuttontrigger" disabled>
  Send
</button>
0

If you just want to validate an email address, you can use the validation that's built into HTML:

<form onsubmit="return false;">
    <input type="email" required="1">
    <input type="submit">
</form>

(Leave out the onsubmit for your own form, of course. It's only in my example to keep you from leaving the page with the form.)

-2

I also searched on the Internet and use this one and it's working.

// email validation
checkEmail = (inputvalue) => {
    const pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    if (pattern.test(inputvalue)) return true;
    return false;
}
gre_gor
  • 6,669
  • 9
  • 47
  • 52
  • 1
    If you copied this code from somewhere, can you please [provide the name of the original author and a link for reference](/help/referencing)? – Andrew Myers Oct 21 '17 at 01:48