midnite Posted December 1, 2007 Share Posted December 1, 2007 at the topic says, i was just bothered for hours on debugging this stuff!! i bet no other stuff can be more weird than this!!i put here for a caution or reminder for those who are working on form validations.i dont want anyone spending their life on this foolish stuff again.i use clear HTML (no inline JS) andwindow.onload = function (){ document.getElementById("youFormId").onsubmit = validate; };to add the event handlerto be easy, u may trywindow.onload = function (){ document.getElementById("youFormId").onsubmit = function() { return 0; }; };the form WILL SUBMIT regardless your ZERO!!!i believe either you will get crazy or you would like to fxxk JS!! at least i doi have only this finding and i have no idea why it happenscan anyone say a few words about this? Link to comment Share on other sites More sharing options...
Ingolme Posted December 1, 2007 Share Posted December 1, 2007 Why not return "false"? Zero (0) is number data. Link to comment Share on other sites More sharing options...
boen_robot Posted December 1, 2007 Share Posted December 1, 2007 Tried returning "false" instead of "0"?And, why use window.onload for this anyway? Why not load the JS after the form has been downloaded (or right after the opening <form> tag) and use 'document.getElementById("youFormId").onsubmit = validate;' directly then. Link to comment Share on other sites More sharing options...
midnite Posted December 1, 2007 Author Share Posted December 1, 2007 using "false" can trap the form of course. Tried returning "false" instead of "0"?And, why use window.onload for this anyway? Why not load the JS after the form has been downloaded (or right after the opening <form> tag) and use 'document.getElementById("youFormId").onsubmit = validate;' directly then.what are you talking about?use <form onsubmit="return validate();" ...> ?it is because i would like to separate JS from HTML totally =)why i have such a problem of 0 (zero)?because i have different checking functions in validate()for instant:function validate() { var valid = true; valid &= checkName(); valid &= checkPhone(); valid &= checkOtherThingsElse(); return valud;} i know using &= is not so proper as it means valid = valid & ... (a bitwise one)but it makes the code shorter.and it means sense to use a bitwise & for boolean, right? at least i think so.moreover, everyone should assume that (zero) should be equivalent to "false" when checking boolean, arent you? Link to comment Share on other sites More sharing options...
Ingolme Posted December 1, 2007 Share Posted December 1, 2007 But there's nor specification that it's actually checking Boolean. In PHP it would work, but it doesn't in Javascript.Try this: function validate() { var valid = true; valid &= checkName(); valid &= checkPhone(); valid &= checkOtherThingsElse(); if(valid == 0) return false; return true;} Link to comment Share on other sites More sharing options...
midnite Posted December 1, 2007 Author Share Posted December 1, 2007 But there's nor specification that it's actually checking Boolean. In PHP it would work, but it doesn't in Javascript.Try this:function validate() { var valid = true; valid &= checkName(); valid &= checkPhone(); valid &= checkOtherThingsElse(); if(valid == 0) return false; return true;} thanks Ingolme. yes, i did try this. but if JS really need us to do this, it is too stupid and it violated my original intention - shorten and keep the code neat.but i tried valid = valid && true; it still remains as a Number (by typeof) ... so i compromised. i used valid = valid && ...it does not work even if i valid = new Boolean(valid); sigh.... (an Object is true to the form onsubmit too)i think JS do well with this, just the HTML form accept only "false" and regard any other stuffs as true. Link to comment Share on other sites More sharing options...
jesh Posted December 3, 2007 Share Posted December 3, 2007 If short code is your primary goal, and you want to stick with the bitwise comparisons, then this may help: return (valid == 1); Link to comment Share on other sites More sharing options...
boen_robot Posted December 3, 2007 Share Posted December 3, 2007 what are you talking about?use <form onsubmit="return validate();" ...> ?it is because i would like to separate JS from HTML totally =)Don't insult me . Would I suggest that you revert to the worse practice?What I meant is to have an HTML like<form ...>...</form><script type="text/javascript" href="script.js"></script> and in the end of the JS, have just document.getElementById("youFormId").onsubmit = validate; That way, you don't need to wait for the "onload" event, because the form has already been loaded. The page also performs better because the browser renders the form before applying JavaScript to it. Don't worry about the possibility of the form being submitted without validation. The server must perform this check as well anyway. Link to comment Share on other sites More sharing options...
midnite Posted December 3, 2007 Author Share Posted December 3, 2007 If short code is your primary goal, and you want to stick with the bitwise comparisons, then this may help:return (valid == 1); oh, COOL!!!i m idiot that i have never think about this >< Link to comment Share on other sites More sharing options...
midnite Posted December 4, 2007 Author Share Posted December 4, 2007 Don't insult me . Would I suggest that you revert to the worse practice?What I meant is to have an HTML like<form ...>...</form><script type="text/javascript" href="script.js"></script> and in the end of the JS, have just document.getElementById("youFormId").onsubmit = validate; That way, you don't need to wait for the "onload" event, because the form has already been loaded. The page also performs better because the browser renders the form before applying JavaScript to it. Don't worry about the possibility of the form being submitted without validation. The server must perform this check as well anyway. haha, my friend. i am not saying which method is good and which it bad. all codes that work are good codes =) Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.