-3

I am wanting to make validation to my Email form. Unfortunately I cannot get this piece of code to work. When I am clicking submit without entering any data in to the form it will send.

I am wanting it not to send without the right Characters in the Email

I have attached a screenshot below of my code

mplungjan
  • 169,008
  • 28
  • 173
  • 236
thjef
  • 39
  • 4
  • 3
    Could you insert your code as text in your post, instead of as an image? – Jacob Jan 14 '16 at 14:39
  • 1
    so you had time to do a screenshot and upload to imgur, but didn't paste actual code which take LESS time to do. Post your real code. – Funk Forty Niner Jan 14 '16 at 14:39
  • Welcome to SO. Please visit the [help] to see how and what to ask. HINT: Add code and efforts to your question – mplungjan Jan 14 '16 at 14:44
  • When I am adding the code it doesn't set out in the right way. This is why I placed an Image. – thjef Jan 14 '16 at 14:46
  • what is it? {1,} only one?? – devpro Jan 14 '16 at 14:46
  • dont worry about the right way, we will update it.. :p – devpro Jan 14 '16 at 14:47
  • You are missing a bracket for sure. Change to `onsubmit="return validate(this)"` and your function to `function validate(theForm) { var reg...; var address = theForm.emailAddress.value; ...... if (...) { alert(....); theForm.emailAddress.focus(); return false; } return true; }` - and never call anything `name="submit"` - lastly read how to format here: http://stackoverflow.com/editing-help – mplungjan Jan 14 '16 at 14:47
  • Possibile duplicate? http://stackoverflow.com/questions/46155/validate-email-address-in-javascript – Lukaszaq Jan 14 '16 at 14:58

1 Answers1

0

You need to change your regex as:

var email_regex = /^[\w%_\-.\d]+@[\w.\-]+.[A-Za-z]{2,6}$/; // reg ex email check

if(emailAddress == "") 
{
    // error if empty
} 
else 
{
    if(!email_regex.test(emailAddress))
    { 
        // invalid error
    }   
    else
    {
        // success
    }
}
devpro
  • 16,184
  • 3
  • 27
  • 38