0

Basically I created a form in html and when things are input properly it simply goes to google.com. Right now I have completed the first few fields but I am unsure of how I would make it recognize if the input that was put in and did not include an @ sign or a . some point after it.

I created a fiddle as I was having trouble getting some of the longer of my lines of code to be in-line.

Click here for the fiddle Example

royki
  • 1,593
  • 3
  • 25
  • 45
Mark Gregg
  • 83
  • 1
  • 2
  • 10

3 Answers3

1

You can use HTML5 properties like :

  • pattern attribute which contains a regexp
  • set your input type to email

If you want to do it with JavaScript, use a regexp also and use the test() method to verify it.

Dr. Z
  • 236
  • 3
  • 19
  • 1
    HTML5 is not supported in all browser's version. I could rant about IE6/7/8 and IE9/10 not being released on Windows XP. – Deep Kakkar Apr 14 '15 at 10:50
  • The old fashion way ... :-) Try to use regexp in JavaScript, but i suggest you to validate your field on the server side too. – Dr. Z Apr 14 '15 at 10:51
1

Add this

 var re = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
    return re.test(email);
Shayan Ghosh
  • 882
  • 6
  • 14
1

You have a few options depending on how thorougly you want to validate.

email.indexOf('@') >= 0

checks that there is an @ at all in the email. See http://jsfiddle.net/27f1h6ws/ for a version of your fiddle with it added.

A more thorough way would be to check it with regex. You can do it extremely simple just checking the general structure of the email input, or extremely thorough check for all valid characters, depending on how crucial the validation is. See this link or the answers in this question for more information.

Community
  • 1
  • 1
Robin
  • 1,251
  • 11
  • 18
  • i am trying this now 1 problem I just realized I had was i had isNaN attached to email and it caused it to always think I was inputting something wrong. – Mark Gregg Apr 14 '15 at 11:02