javascript - using id="submit" twice on one page -
i creating login/register part site. , login , register forms on page.
like:
<form name="loginform" style="text-align:center;" method="post" onsubmit="return validateform();" action="index.php"> <div class="row"> <input type="text" name="email" id="email" autocomplete="off" placeholder="email address" /> </div> <br /> <div class="row"> <input type="password" name="password" id="password" autocomplete="off" placeholder="password" /> </div> <br /> <div class="row"> <button id="submit" type="submit" class="button large arrow-type-2 dark">log me in</button> </div> </form> <form name="registerform" style="text-align:center;" method="post" onsubmit="return validatethisform();" action="index.php"> <div class="row"> <input type="text" name="email" id="email2" autocomplete="off" placeholder="email address"/> </div> <br /> <div class="row"> <input type="password" name="password" id="password2" autocomplete="off" placeholder="password"/> </div> <br /> <div class="row"> <button id="submit" type="submit" class="button large arrow-type-2 dark">create free account</button> </div> </form>
my js validation is: ( needs work )
function validateform() { var x=document.forms["loginform"]["email"].value; var atpos=x.indexof("@"); var dotpos=x.lastindexof("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { alert("not valid e-mail address"); return false; } var x=document.forms["loginform"]["password"].value; if (x==null || x=="") { alert("please enter password"); return false; } } function validatethisform() { var x=document.forms["registerform"]["email2"].value; var atpos=x.indexof("@"); var dotpos=x.lastindexof("."); if (atpos<1 || dotpos<atpos+2 || dotpos+2>=x.length) { alert("not valid e-mail address"); return false; } var x=document.forms["registerform"]["password2"].value; if (x==null || x=="") { alert("please enter password"); return false; } }
the issue have page validation, works perfect. because have duplicate submit id's , need clean up.
can offer suggestions on improving code above ?
/////////////////////////////////////////
using: code below cross browser placeholder
$('[placeholder]').focus(function() { var input = $(this); if (input.val() == input.attr('placeholder')) { input.val(''); input.removeclass('placeholder'); } }).blur(function() { var input = $(this); if (input.val() == '' || input.val() == input.attr('placeholder')) { input.addclass('placeholder'); input.val(input.attr('placeholder')); } }).blur().parents('form').submit(function() { $(this).find('[placeholder]').each(function() { var input = $(this); if (input.val() == input.attr('placeholder')) { input.val(''); } }) });
i simplified html code following:
<form name="loginform" method="post" onsubmit="return validateform();" action="index.php"> <label>email address: <input type="email" name="email" autocomplete="off" placeholder="email address"/></label> <label>password: <input type="password" name="password" placeholder="password"/></label> <button type="submit" class="button large arrow-type-2 dark">log in</button> </form> <form name="registerform" method="post" onsubmit="return validatethisform();" action="index.php"> <label>email address: <input type="email" name="email" autocomplete="off" placeholder="email address"/></label> <label>password: <input type="password" name="password" placeholder="password"/></label> <button type="submit" class="button large arrow-type-2 dark">create free account</button> </form>
points
- always include label. not browsers support html5 placeholders.
all ids here reluctant. forms can accessed by
var loginform = document.forms.loginform; //by name
and form elements
loginform.email; //also name
- no need
div
s ,br
s manage line breaks. use labels themselves. adddisplay: block;
necessary. - don't use inline style attribute. use css
<style>
element or external stylesheet. - there's no autocomplete on password fields.
- use html5's new form input types.
type="email"
have browser natively validate field , notify user if email not valid. - keep simple. no need bloating.
Comments
Post a Comment