javascript - "Uncaught ReferenceError: fail is not defined." Why is this happening? -
i in process of making dynamic signup form.
the form has been created , various functions validating entries have been written.
however, variable have created returning error message on failure of vaildation has come in javascript console not being defined.
i've used strict methods , have stated in globals declaration defining variable within following code.
what i'm trying use javascript validate entries within form , prompt user correct errors before form submitted database.
the error code returned javascript console follows: uncaught referenceerror: fail not defined. line 5 validate.js
i've attached code html form , validation.js file, hope i've explained in way can understood.
here code validation.html
:
<html> <head> <title>an example form</title> <style type="text/css"> .signup { border: 1px solid #999999; font: normal 14px helvetica; color: #444444; } </style> <script language="javascript" type="text/javascript" src="validate.js"></script> </head> <body> <table class="signup" cellpadding="2" cellspacing="5" bgcolor="#eeeeee"> <th colspan="2" align="center">signup form</th> <form method="post" action="adduser.php" onsubmit="validateform(this)"> <tr> <td>forename</td> <td><input type="text" maxlength="32" name="forename"/></td> </tr> <tr> <td>surname</td> <td><input type="text" maxlength="32" name="surname"/></td> </tr> <tr> <td>username</td> <td><input type="text" maxlength="16" name="username"/></td> </tr> <tr> <td>password</td> <td><input type="text" maxlength="12" name="password"/></td> </tr> <tr> <td>age</td> <td><input type="text" maxlength="2" name="age"/></td> </tr> <tr> <td>e-mail</td> <td><input type="text" maxlength="64" name="email"/></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="signup"/></td> </tr> </form> </table> </body> </html>
here code validate.js
:
/*jslint browser: true*/ /*global fail:true, alert:true, validateforename, validatesurname, validateusername, validatepassword, validateage, validateemail*/ function validateform(form) { "use strict"; fail = validateforename(form.forename.value); fail += validatesurname(form.surname.value); fail += validateusername(form.username.value); fail += validatepassword(form.password.value); fail += validateage(form.age.value); fail += validateemail(form.email.value); if (fail === "") { return true; } else { alert(fail); return false; } } function validateforename(field) { "use strict"; if (field === "") { return "no forename entered.\n"; } return ""; } function validatesurname(field) { "use strict"; if (field === "") { return "no surname entered.\n"; } return ""; } function validateusername(field) { "use strict"; if (field === "") { return "no username entered.\n"; } else if (field.length < 5) { return "usernames must @ least 5 characters.\n"; } else if (/[\^a-za-z0-9_\-]/.test(field)) { return "only a-z, a-z, 0-9, - , _ allowed in usernames.\n"; } return ""; } function validatepassword(field) { "use strict"; if (field === "") { return "no password entered.\n"; } else if (field.length < 6) { return "passwords must @ least 6 characters.\n"; } else if (!/[a-z]/.test(field) || !/[a-z]/.test(field) || !/[0-9]/.test(field)) { return "passwords require 1 each of a-z, a-z , 0-9.\n"; } return ""; } function validateage(field) { "use strict"; if (isnan(field)) { return "no age entered.\n"; } else if (field < 18 || field > 99) { return "age must between 18 , 99.\n"; } return ""; } function validateemail(field) { "use strict"; if (field === "") { return "no e-mail entered.\n"; } else if (!(((field.indexof(".") > 0) && (field.indexof("@") > 0)) || /[\^a-za-z0-9.@_\-]/.test(field))) { return "the email address invalid.\n"; } return ""; }
the /* global */
comment tells jslint global variables have been defined in other files.
not declare anything.
you need declare variables, using var
statement.
note want locals, not globals.
in fact, shouldn't using /* global
@ all, unless have globals file (which don't).
browser standard methods alert()
known because of browser: true
, , jslint sees declared in file.
Comments
Post a Comment