html - Basic JavaScript validation not working -
i'm trying make simple form javascript validation. has 4 fields: title, email address, rating , comments. far know, code have here should work validate form , should not allowed submit whenever press submit button none of prompts appear , browser submits form. let me know going wrong please? i'd imagine there simple solution or forgetting i'm pretty new apologies if it's obvious.
the html , javascript code is:
<html> <head> <script type="text/javascript"> function validateform() { var e=document.forms["review"]["title"].value; var s=document.forms["review"]["email"].value; var t=document.forms["review"]["rating"].value; var c=document.forms["review"]["comments"].value; var atsym=s.indexof("@"); var dotsym=s.lastindexof("."); if (e==null || e=="") { document.getelementbyid("valalert").innerhtml="please enter title"; return false; } else if (s==null || s=="" || atsym<1 || dotsym<atsym+2 || dotsym+2>=s.length) { document.getelementbyid("valalert").innerhtml="that not valid email address!"; return false; } else if (t=="0") { document.getelementbyid("valalert").innerhtml="you must enter rating"; return false; } else if (c==null || c=="") { document.getelementbyid("valalert").innerhtml="you need enter kind of comment."; return false; } else { alert ("your review of " + t + "has been submitted!"); } } </script> </head> <body> <div id="valalert"></div> <form name="review" onsubmit="return validateform()"> <fieldset> enter title: <input type="text" name="title"> </br> </br> enter email address: <input type="text" name="email"> </br> </br> please enter rating: <select name="rating"> <option value="0">(please select rating)</option> <option value="1s">1 star</option> <option value="2s">2 stars</option> <option value="3s">3 stars</option> <option value="4s">4 stars</option> <option value="5s">5 stars!</option> </select> </br> </br> <textarea name="comments" rows="8" colspan="40">comments:</textarea> </fieldset> </br> </br> <fieldset> <input class="button" type="submit" value="submit"/> </fieldset> </form> </body>
please make null value check before doing operations below
var dotsym=s.lastindexof(".");
add null check variable 's'.please check function naming convention below
obj.lastindexof(); obj.lastindexof(".");
Comments
Post a Comment