javascript - Double Alert Windows -
i getting double alert prompts single form: alert pops first time, when click ok, same 1 pops open again. think code should alert once when hits "else window.alert(samewarning) , stop when reaches "return false()"; seems return false() causing alert twice. maybe because have 2 forms on 1 page; yet, have 2 separate function names ("signinform()" , "savesignup()")and call 2 separate forms (forms[0] , forms[1])? think code correct, can't figure out @ point. appreciated. in advance!
<script type="text/javascript"> //first function forms[0] function signinform(){ // var samewarning = "the required fields not submitted following error(s). \n \n"; var warninglogin = "the following field(s) require entry: \n \n"; var samewarning = warninglogin; var username = document.forms[0].username.value; var pswd = document.forms[0].pswd.value; if(username == ""){ samewarning += " - first name \n"; } if(pswd == ""){ samewarning += " - password \n"; } if (warninglogin == samewarning){ return true; } else { window.alert(samewarning); } return false; } //second function call forms[1] function savesignup(){ // var warning = "the required fields not submitted following error(s). \n \n"; var warning = "the following field(s) require entry: \n \n"; var same = warning; var firstname = document.forms[1].firstname.value; var lastname = document.forms[1].lastname.value; var email = document.forms[1].email.value; var phone = document.forms[1].phonenumber.value; if(firstname == ""){ //firstname.style.backgroundcolor="red"; warning += " - first name \n"; } if(lastname == ""){ warning += " - last name \n"; } if(email == ""){ warning += " - email \n"; } if(phone == ""){ warning += " - work phone \n"; } if(phone < 5 ){ warning += " - must numberssss \n"; } if (warning == same){ return true; } else { alert(warning); } return false; } </script>
html
<h1>login</h1> <form method="post" onsubmit="return signinform();" action="" > <input type="text" placeholder="username/email" name="username"> <input type="password" placeholder="password" name="pswd" id="pswd"> <input type="submit" onclick="signinform();" value="sign in"> </form> <h1>sign up!</h1> <form method="post" onsubmit="return savesignup()" action="" > <input type="text" placeholder="first name" name="firstname"> <input type="text" placeholder="last name" name="lastname"> <input type="text" placeholder="email" name="email"> <input type="text" placeholder="phone number" name="phonenumber"> <input type="submit" onclick="savesignup();" class="button wide" value="request account">
when click submit, first fires click event (calling function), fires submit event (calling function again). remove onclick submit buttons.
Comments
Post a Comment