Simple Javascript for form validation doesn't work -
so, have written form , simple javascript validate user input. thing is, works until add third function, validatehash, takes hashtag inputed textbox , checks if has hashtag @ beginning. sent code friend, , said on pc works fine (checks if fields filled, , checks if hashtag correct), while on mine skips every function , submits data php page without checking anything. using firefox, , he's using chrome, tried latter same results. there wrong in code itself?
<!doctype html> <meta charset="utf-8"> <link rel="stylesheet" href="style.css" /> <script language="javascript" type="text/javascript"> function validateform() { if(validate(document.form.song.value) && validate(document.form.hashtag.value)) if(validatehash(document.form.hashtag.value)) return true; else { alert("please fill fields"); return false; } } function validate(text) { if(text==null || text=="") return false; else return true; } function validatehash(text) { if(text.charat(0) != "#") { alert("insert hashtag correctly"); return false; } else return true; } </script> <form action="format.php" name="form" method="post" class="basic-grey" onsubmit="return validateform()"> <h1>anisongs form <span>fill text in fields</span> </h1> <label> <span>song lyrics :</span> <textarea id="song" name="song" placeholder="max 120 characters" maxlength="120"></textarea> </label> <label> <span>anime hashtag :</span> <input id="hashtag" type="text" name="hashtag" placeholder="#loghorizon" maxlength="20"/> </label> <span> </span> <input type="submit" class="button" value="submit" /> <input type="reset" class="button" value="cancel" /> </form>
well, issues brackets. js fancies them, , without them on single lines ifs wouldn't start work.
Comments
Post a Comment