javascript - Form email validation not working properly -
i have form i'm trying validate, email, select country fields seems not being validated. can 1 tell me problem?
here javascript:
<script type="text/javascript"> function validateemail() { var emailid = document.form1.email.value; atpos = emailid.indexof("@"); dotpos = emailid.lastindexof("."); if (atpos < 1 || ( dotpos - atpos < 2 )) { alert("please enter correct email id") document.form1.email.focus() ; return false; } return( true ); } function validate() { //username password first_n last_n company mobile email residence if( document.form1.first_n.value == "" ) { alert( "please enter first name!" ); document.form1.first_n.focus() ; return false; } if( document.form1.last_n.value == "" ) { alert( "please enter last name!" ); document.form1.last_n.focus() ; return false; } if( document.form1.username.value == "" ) { alert( "please enter username!" ); document.form1.username.focus() ; return false; } if( document.form1.password.value == "" ) { alert( "please enter password!" ); document.form1.password.focus() ; return false; } if( document.form1.company.value == "" ) { alert( "please provide company name!" ); document.form1.company.focus() ; return false; } if( document.form1.mobile.value == "" ) { alert( "please provide mobile number!" ); document.form1.mobile.focus() ; return false; } if( document.form1.email.value == "" ) { alert( "please provide email!" ); document.form1.email.focus() ; return false; } if( document.form1.email.value != "" ) { // put check data format var ret = validateemail(); if( ret == false ) { return false; }else return true; } if( document.form1.zip.value == "" || isnan( document.form1.zip.value ) || document.form1.zip.value.length != 5 ) { alert( "please provide zip in format #####." ); document.form1.zip.focus() ; return false; } if( document.form1.residence.value == "-1" ) { alert( "please provide country!" ); return false; } return( true ); } </script>
and form html:
<form name="form1" method="post" action="add_new.php" onsubmit="return(validate());"> <div style="clear:both;padding:0px 10px 0 10px;margin-bottom:20px;"> <h5>interested in</h5> <input id="toggle-on" class="toggle toggle-left" name="toggle" value="false" type="radio" checked> <label for="toggle-on" class="btn">hosting</label> <input id="toggle-off" class="toggle toggle-right" name="toggle" value="true" type="radio" ><label for="toggle-off" class="btn">email accounts</label> </div> <div style="clear-both;"> <input name="first_n" type="text" placeholder="first name" class="login-text-lbl-pink-no-width" id="first_n"> <input name="last_n" type="text" placeholder="last name" class="login-text-lbl-pink-no-width" id="last_n"> </div> <input name="username" type="text" placeholder="username" class="login-text-lbl-pink-no-width" id="username"><br/> <input name="password" type="password" placeholder="password" class="login-text-lbl-pink-no-width" id="password"><br/> <input name="company" type="text" placeholder="company" class="login-text-lbl-pink-odd" id="company"> <br/> <input name="mobile" type="text" placeholder="mobile phone" id="login-text-lbl" class="pink-transparent-item" id="mobile"> <br/> <input name="email" type="text" placeholder="email" class="login-text-lbl-pink-odd" class="pink-transparent-item" id="email"> <br/> <select name="residence" id="residence" id="login-text-lbl" style=" background-color: rgba(240, 96, 96, 0.62); border: none; -webkit-appearance: none; -moz-appearance: none; appearance: none; height: 30px; margin: 5px; font-style: italic; width: 90%; padding: 5px; color: #34584b; float: none;" > <option value="-1" selected>[choose country]</option> <option value="us">america</option> <option value="de">germany</option> <option value="it">italy</option> <option value="hk">hong kong</option><br/> <input name="domain" class="pink-transparent-item" type="text" placeholder="existing domain" id="login-text-lbl" id="domain"> <br/> <input type="submit" name="submit" value="submit" style='font-family: "neo sans light", verdana, tahoma;' class="login-button-pink"> </form>
there's few things wrong here, let's try , answer questions -
email works fine me in chrome , firefox, though can't speak safari, ie, seamonkey, etc.
your validateemail()
method has input named email
- won't it. js case-sensitive.
even after validate, you'll never past email because of return true
have after bringing value of validateemail()
. you've left validate()
@ point.
as other things - should run code through jslint check errors. see couple unclosed braces. jslint because seems you're relative beginner @ js, need more fundamentalist approach crockford brings code, @ least little while until @ (no offense intended; start @ beginning).
Comments
Post a Comment