1

Been making a simple form + data validation thing, I'm using indexof to make sure there is a "@" and "." in the email address, but it is always returning -1.

var custEmail = document.getElementById("custEmail"); if (custEmail.value == "" || custEmail.value.indexOf("@" == -1) || custEmail.value.indexOf("." == -1)) { alert("You must enter a valid Email address!\n" } 

I've tried changing the indexof check to different letters instead of the symbols, but it still returns -1, which makes me think I'm trying to get the data incorrectly.

2
  • 4
    it's custEmail.value.indexOf("@") == -1, not indexOf("@" == -1) Commented May 29, 2016 at 4:18
  • 2
    Oh my god I'm an idiot. I blame lack of sleep. This was it. Thanks. Commented May 29, 2016 at 4:20

2 Answers 2

3

your parens are in the wrong place

custEmail.value.indexOf("@") == -1 || custEmail.value.indexOf(".") == -1 
Sign up to request clarification or add additional context in comments.

Comments

0

You can use HTML5 input type email rather than writing validations yourself

<input type="email"/> 

2 Comments

This is more of a skills exercise than something that is supposed to be completely functional, which is why I wanted to do it manually.
Okay great, you also use regex to do validations like these as a part of skill development

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.