0

I need to add to add the following characteristics to this form but I'm stuck. any help would b greatly appreciated. Thanks

  1. One or more word characters
  2. Exactly one at-sign
  3. One or more word characters
  4. Exactly one period Two or more characters that are a-z, A-Z, 0-9, period, or hyphen

    <!DOCTYPE html>
      <html>
      <head>
      <script>
        function validateForm()
        {
          var x=document.forms["myForm"]["email"].value;
          var atpos=x.indexOf("@");
          var dotpos=x.lastIndexOf(".");
          if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length)
          {
             alert("Not a valid e-mail address");
             return false;
          }
        }
      </script>
    </head>
    
    <body>
      <form name="myForm" action="demo_form.asp" onsubmit="return validateForm();" method="post">
        Email: <input type="text" name="email">
        <input type="submit" value="Submit">
      </form>
    </body>
    

bneely
  • 9,083
  • 4
  • 38
  • 46
UnPatoCuacCuac
  • 315
  • 2
  • 7
  • 18

3 Answers3

1
function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
    return pattern.test(emailAddress);
};

This function will work in your case

To call it like this

if( !isValidEmailAddress( emailaddress ) ) { /* do stuff here */ }

keep in mind that no 100% regex email check exists!

Checkout this link

https://stackoverflow.com/a/2855946/2630817

Community
  • 1
  • 1
Just code
  • 13,553
  • 10
  • 51
  • 93
  • If you're going to snatch [someone else's answer](http://stackoverflow.com/a/2855946/1338292), you may as well flag this question as a duplicate of the corresponding question. – Ja͢ck Mar 10 '14 at 04:00
  • Hey @jack i have just included link there if user find difficulty to search – Just code Mar 10 '14 at 04:02
0

Try this:

<script>        
   function validateForm(){

       var email=document.forms["myForm"]["email"].value;

       var filter = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

       if (!filter.test(email)) {
           alert("Not a valid e-mail address");
           return false;
       }
       return true;
   }
</script>
suresh gopal
  • 3,138
  • 6
  • 27
  • 58
  • Some explanation would be useful along with the code, especially given that this does not implement the rules that the OP asked for. – nnnnnn Mar 10 '14 at 04:06
  • So this is basically ripped from [this answer](http://stackoverflow.com/a/46181/1338292) and the expression eliminates valid email addresses and should therefore not be used. – Ja͢ck Mar 10 '14 at 04:09
0

Try this pure javascript:

function checkEmail() {

    var email = document.getElementById('txtEmail');
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    if (!filter.test(email.value)) {
    alert('Please provide a valid email address');
    email.focus;
    return false;
 }
Mr.G
  • 3,413
  • 2
  • 16
  • 20