admin管理员组文章数量:1426855
I am trying to make a sign-up form that will do client-side validation (check the correct layout of email and matching passwords) before sending any data to the server. I have been having trouble checking to see if the email is in the correct form. I can't get the if(email.indexOf(@))
to work correctly. I think I misused the .indexOf()
This is my JavaScript:
function sign_check() {
var email = document.getElementById("sign_email").value;
var user = document.getElementById("sign_user").value;
var pass = document.getElementById("sign_pass").value;
var passcon = document.getElementById("sign_confirm").value;
if(pass !== passcon){
document.getElementById("sign_alert").innerHTML="The passwords do not match"
}
//This part determines whether or not to send the data to the server
if(email.length >= 7){
if(email.indexOf("@")){
if(user.length >= 1){
if(pass.length >= 1){
if(passcon.length >= 1){
if(pass === passcon){
alert("All of the requirements have been met")
}
}
}
}
}
}
}
And this is my html:
<h1 id="pop_up" class="pop_up">Sign Up</h1>
<form id="sign_up" class="sign_up">
<label id="alert_s1" class="alert"> <br /> </label>
<input id="sign_email" class="sign" type="text" placeholder="Email" name="sign_email" /><br />
<label id="alert_s2" class="alert"> <br /> </label>
<input id="sign_user" class="sign" type="text" placeholder="Username" name="sign_user" /><br />
<label id="alert_s3" class="alert"> <br /> </label>
<input id="sign_pass" class="sign" type="text" placeholder="Password" name="sign_pass" /><br />
<label id="alert_s4" class="alert"> <br /> </label>
<input id="sign_confirm" class="sign" type="text" placeholder="Confirm Password" name="sign_confirm" />
</form>
<p id="sign_alert" class="alert"></p>
<button onclick="sign_check()">Submit</button>
<a href="javascript:void(0)" class="pop_up" id="pop_up" onclick="document.getElementById('sign_box').style.display='none'; document.getElementById('log_box').style.display='block'">Already have an acount? Click here to log in.</a>
</div>
I am trying to make a sign-up form that will do client-side validation (check the correct layout of email and matching passwords) before sending any data to the server. I have been having trouble checking to see if the email is in the correct form. I can't get the if(email.indexOf(@))
to work correctly. I think I misused the .indexOf()
This is my JavaScript:
function sign_check() {
var email = document.getElementById("sign_email").value;
var user = document.getElementById("sign_user").value;
var pass = document.getElementById("sign_pass").value;
var passcon = document.getElementById("sign_confirm").value;
if(pass !== passcon){
document.getElementById("sign_alert").innerHTML="The passwords do not match"
}
//This part determines whether or not to send the data to the server
if(email.length >= 7){
if(email.indexOf("@")){
if(user.length >= 1){
if(pass.length >= 1){
if(passcon.length >= 1){
if(pass === passcon){
alert("All of the requirements have been met")
}
}
}
}
}
}
}
And this is my html:
<h1 id="pop_up" class="pop_up">Sign Up</h1>
<form id="sign_up" class="sign_up">
<label id="alert_s1" class="alert"> <br /> </label>
<input id="sign_email" class="sign" type="text" placeholder="Email" name="sign_email" /><br />
<label id="alert_s2" class="alert"> <br /> </label>
<input id="sign_user" class="sign" type="text" placeholder="Username" name="sign_user" /><br />
<label id="alert_s3" class="alert"> <br /> </label>
<input id="sign_pass" class="sign" type="text" placeholder="Password" name="sign_pass" /><br />
<label id="alert_s4" class="alert"> <br /> </label>
<input id="sign_confirm" class="sign" type="text" placeholder="Confirm Password" name="sign_confirm" />
</form>
<p id="sign_alert" class="alert"></p>
<button onclick="sign_check()">Submit</button>
<a href="javascript:void(0)" class="pop_up" id="pop_up" onclick="document.getElementById('sign_box').style.display='none'; document.getElementById('log_box').style.display='block'">Already have an acount? Click here to log in.</a>
</div>
Share
Improve this question
asked Mar 9, 2014 at 0:06
justephensjustephens
31 gold badge1 silver badge2 bronze badges
5
-
2
0
is a valid index, and-1
is returned on failure, so:email.indexOf("@") !== -1
Or do> 0
if you want to ensure that@
isn't the first character. – cookie monster Commented Mar 9, 2014 at 0:08 - holy nested if statements, batman! – PlantTheIdea Commented Mar 9, 2014 at 0:09
-
4
How bout just
/^\S+@\S+\.\S+$/.test(email)
– adeneo Commented Mar 9, 2014 at 0:09 - Not a fan of the && operator? – leigero Commented Mar 9, 2014 at 0:11
- 1 Link to a better way to validate e-mail in JavaScript stackoverflow./questions/46155/… It's only got 827 points, who knows... – Brian Hoover Commented Mar 9, 2014 at 0:12
2 Answers
Reset to default 3First, your method to validate the email is not very accurate :) Besides that, you're using indexOf
incorrectly. Use this instead.
if(email.indexOf("@") != -1 )
You used double quotes ""
instead of ''
in the ('@')
. That should make it work. And ===
actually works too. The quotation marks were all you needed to change. I hope this helps!
本文标签: javascriptValidating email using indexOf()Stack Overflow
版权声明:本文标题:javascript - Validating email using indexOf() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745483473a2660278.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论