php - Jquery form post errors -
i new in jquery. i'm working on form. title , text (textarea) when fill out fields , click send, 2 alerts appear when click ok 2 times scroll page up. fill in fields , starts again beginning.
how can ensure after 2 checks continues page?
jquery:
$("#formpages").submit( function(event) { var title = $('#title').val(); var text = $('#text').val(); if(title == ''){ $("#titleerror").html("<p>fill title</p>"); alert('title empty'); } if(text == ''){ $("#texterror").html("<p>fill text</p>"); alert('text empty'); } $("html, body").animate({ scrolltop: 0 }, 600); event.preventdefault(); });
form:
<form class="form" id="formpages"> <div> <label class="title">title</label> <div id="titleerror"></div> <input type="text" id="title" name="title" value=""> </div> <div> <label class="title">text</label> <div id="texterror"></div> <textarea name="text" id="text" rows="14" cols="50"></textarea><br /> </div> <div> <input class="btn btn-success" type="submit" id="submitbutton" name="submitbutton" value="submit"> </div> </form>
you should not use event.preventdefault();
when outside of if statements. code should work this:
$("#formpages").submit( function(event) { var title = $('#title').val(); var text = $('#text').val(); if(title == ''){ $("#titleerror").html("<p>fill title</p>"); alert('title empty'); event.preventdefault(); } if(text == ''){ $("#texterror").html("<p>fill text</p>"); alert('text empty'); event.preventdefault(); } $("html, body").animate({ scrolltop: 0 }, 600); });
have not tested should work expected, comment if errors. check if textarea or title empty better use if (text.length === 0)
, if (title.length === 0)
edit
please check jsfiddle here working example, have tested chrome, ff, , ie(i know :d)
Comments
Post a Comment