Validate html form with javascript -
i want validate user input javascript before sending data server, when try browser doesn't display alert message.
i cannot find problem. please me. thank you!
my html:
<title>login page</title> <script type="text/javascript" src="../js/functions.js"></script> </head> <body> <div id="content"> <form name="signin_form" action="../processes/login_process.php" onsubmit="return checklogindata()" method="post"> <h1>accesso utente</h1> <p> <label for='tf_nick'>nick</label> <input type='text' id='tf_nick' name='tf_nick' value=""> <label for='tf_password'>password</label> <input type='password' id='tf_password' name='tf_password' value=''> <input type='submit' value='ok'> <input type='reset' value='pulisci'> </p> </form> </div> </body>
my javascript:
function checklogindata(){ var formobj = document.signin_form; var nick = "" + formobj.tf_nick.value; var pass = "" + formobj.tf_password.value; if ( nick == "" ) { alert("inserisci il nick per accedere!"); return false; } else if ( pass == "" ) { alert("inserisci la password per accedere!"); return false; } else { var regexnick = /(?=.*[0-9])(?=.*[a-za-z%])^[a-za-z0-9%]{3,6}$/; var regexpass = /(?=.*[a-z])(?=.*[a-z])^[a-za-z]{4,8}$/; if ( !regexnick.test(nick) ){ alert("nick non valido!"); return false; } else if ( !regexpass.test(pass) ) { alert("password non valida!"); return false; } else { return true; } } }
this file system:
main_folder/pages/login.php main_folder/js/functions.js
i created jsfiddle copy/pasted code , there works fine: http://jsfiddle.net/872d6ssn/1/ (code included below because stackoverflow demands that, it's copy)
are sure include js file in correct way? seems you're not including file @ all.
html
<h1>accesso utente</h1> <p> <label for='tf_nick'>nick</label> <input type='text' id='tf_nick' name='tf_nick' value=""> <label for='tf_password'>password</label> <input type='password' id='tf_password' name='tf_password' value=''> <input type='submit' value='ok'> <input type='reset' value='pulisci'> </p> </form>
javascript
function checklogindata(){ var formobj = document.signin_form; var nick = "" + formobj.tf_nick.value; var pass = "" + formobj.tf_password.value; if ( nick == "" ) { alert("inserisci il nick per accedere!"); return false; } else if ( pass == "" ) { alert("inserisci la password per accedere!"); return false; } else { var regexnick = /(?=.*[0-9])(?=.*[a-za-z%])^[a-za-z0-9%]{3,6}$/; var regexpass = /(?=.*[a-z])(?=.*[a-z])^[a-za-z]{4,8}$/; if ( !regexnick.test(nick) ){ alert("nick non valido!"); return false; } else if ( !regexpass.test(pass) ) { alert("password non valida!"); return false; } else { return true; } } }
Comments
Post a Comment