Click to See Complete Forum and Search --> : form validation with '.js' files
erick30
01-09-2004, 07:12 AM
I have ten ‘.js’ external files in order to validate a form: checkMail.js, checkPassword.js, checkZIP.js, etc.. I do not know which is the best way to trigger them together, now I do it thus:
<script src="Validations/new_user1/E-Mail.js">
</script>
<script src="Validations/new_user1/Password.js">
</script>
<script src="Validations/new_user1/ZIP_code.js">
</script>
etc,…
<form name="new_user1.asp" onSubmit="return checkMail(),checkPassword(),checkZIP(), etc …;" action="new_user2.asp" method="post">
Is this the best way to trigger together ten ‘.js’ external files?
I consider that put it all these validations together in one ‘.js’ file is very confused. Isn’ t it?
Pittimann
01-09-2004, 07:25 AM
Hi!
Why not put all the stuff into one file and maybe in one function??
Irrespective of that: your script tags are lacking the type attribute, and you should use a different name for your form. You just put its' name attribute to "new_user1.asp". When referring to that name, you will be in trouble!
Cheers - Pit
wood_tah
01-09-2004, 07:54 AM
Why not put all of your method calls into one method (e.g. validate()) and add that function to one of the .js files (or its own separate .js file)?
erick30
01-09-2004, 09:12 AM
Thanks Pittmann and wood_tah.
Why not put all of your method calls into one method (e.g. validate()) and add that function to one of the .js files (or its own separate .js file)?
How I must add that function (validate()) to one of the .js files so that trigger all the separate .js files? Could you give me an example please?
wood_tah
01-09-2004, 10:01 AM
Instead of having all of the function calls you need to invoke in your onsubmit handler, just use:
onsubmit="return validate()"
Then add this function to a .js file (if it is new, then include this file as well):
function validate() {
if (!checkMail()) {
alert("some message");
return false;
}
if (!checkPassword()) {
alert("some message");
return false;
}
if (!checkZIP()) {
alert("some message");
return false;
}
return true;
}
The above code assumes you have return true/false in your functions depending on if the validation is successful
This is just a way to clean up your handler, to make it more manageable. All the .js files will be loaded when the page loads, and the functions w/in them will be invoked when validate() is called (assuming the functions in the .js file are being called from validate().
I hope this answers your question... if not, I apologize b/c I must have misinterpreted your original post.
erick30
01-09-2004, 11:02 AM
I understand that with these if's (if (!checkMail()), if (!checkPassword()), if (!checkZIP()) ) you are referring to the functions written in '.js' files. I don' t understand why I need to show a message after call that functions, every function in every '.js' file have different messages to show and I only want to invoke them, for instance (I invent):
function validate() {
exec checkMail()
exec checkPassword()
exec checkZIP()
}
wood_tah
01-09-2004, 11:39 AM
Sorry, that was just some dummy code in case you wanted to show a message telling the user what needed to be corrected... but it appears that you are including that in each function that is being called.
erick30
01-09-2004, 11:58 AM
Yes, but Your function (function validate() {.....) invokes the '.js' functions like 'checkMail(),checkPassword(),checkZIP()' ?
I want invoke theses functions in the 'validate()' function. How can I do it?
Thus ?: (I think not :confused: )
function validate() {
!checkMail()
!checkPassword()
!checkZIP()
}
Pittimann
01-09-2004, 12:04 PM
Hi!
Please leave out the "!" before the names of the functions which you want to call...
Cheers - Pit
wood_tah
01-09-2004, 12:12 PM
To invoke the functions from your various other .js files in the validate() function, all you need to do is call them from within the validate() function, as you would normally call a function.
function validate() {
checkMail();
checkPassword();
checkZIP();
}
Because these are validation functions, I am assuming that you are returning a value of true at the end if all is well, and false if one of the validations fail. Given this scenario, the code sample I originally provided to you will work -- just remove all of the alert statements. This way, if any of the functions that you are calling failed, your validate function will return false, thus stopping the submission until the user can go back and correct their mistakes.
erick30
01-09-2004, 12:17 PM
Ok :D , now runs fine!
Thank you
erick30
01-09-2004, 12:38 PM
Thank you very much wood_tah :) . I am going to try your code later. ;)
I will tell you something !
erick30
01-10-2004, 04:29 AM
You are simpy the best ! :) Your function runs fine !!
Only I wish you would explain to me how the function 'validate()' works.
I don' t understand what the syntax does 'if (!checkMail()) { return false;}'.
What does '!' mean?
function valForm1() {
if (!checkMail()) {
return false;
}
if (!checkZIP()) {
return false;
}
return true;
}
erick30
01-10-2004, 04:36 AM
And a beginner question plus: What does the semicolon ';' does exactly after the (returns) 'return false;' or 'return true;' ?
Pittimann
01-10-2004, 04:46 AM
Hi!
Simply speaking, the "!" can be "translated" with "not". Example: If your function checkMail returns a value, the line in the validate function just means:
if (checkMail does not return true or a value to deal with){ the validate function has to return false}
where the semicolon stands for the end of a line to be executed. Separating with semicolons can also be done without a linebreak in the script. Example:
if (blah==??){execute1;execute2;execute3;...;}
Cheers - Pit
erick30
01-10-2004, 05:18 AM
Ok, thank you ! ;)