smithmr8 Posted March 4, 2009 Share Posted March 4, 2009 Hello, I've got a form validation script, which currently runs when either a field is edited or submitted. I'd like to have it run when the page is loaded. Currently, I am using this for the running whilst submitted.. <form onSubmit="return checkForm();"... I am using the onChange event to run the same function when any field is changed. I'd like to be able to run the function at the load of the page, so its already telling the person what fields need to be filled in. Would greatly appreciate some help.. Regards, Luke Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 4, 2009 Share Posted March 4, 2009 <body onload="functionhere()"> Quote Link to comment Share on other sites More sharing options...
smithmr8 Posted March 4, 2009 Author Share Posted March 4, 2009 Ah, yeah, but I don't really want it to load on every page. Although, I suppose it doesn't really matter. I don't suppose there is a way to get the name of the current page you are looking at is there ? So I can only put that in the body tag when its on that page. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 4, 2009 Share Posted March 4, 2009 OK, I am assuming from your response that you have default code that creates the head and <BODY> tag for the page. It is only in the content of the page that you would *know* what page is being loaded? Well, that is easy enough to work around. You could simple insert this bit of code within the code that builds the form <script type="text/javascript"> window.onload = function() { checkForm(); } </script> But, I think this is a poor approach. JavaScript validation is a great tool to give the user some immediate feedback, but your pages should never totally rely upon JavaScript - especially for validation. I'm assuming you want to validate the page when it loads because some fields will already be populated. I can only think of that occuring if you have server-side validation and are redisplaying the form after server-side validation fails. Well, server-side validation should always be in place (in case user has JS turned off). But, if user has JS turned off and they submit invalid data, what would be the point of returning them to the form and trying to use JS to tell them their errors. You should use the server-side code to display the erros of any data that has been submitted. Quote Link to comment Share on other sites More sharing options...
smithmr8 Posted March 4, 2009 Author Share Posted March 4, 2009 I understand that. There is server side validation aswell as the javascript. The JS is just displaying visual notificiations beside fields as to whether the fields are filled in correctly. I only want to run the function on load, as it will display the messages to start with.. instead of them displaying for everything when someone edits a field as it doesn't look too good. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 4, 2009 Share Posted March 4, 2009 have you checked out Jquery's validation plugin? it does the same thing Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 4, 2009 Share Posted March 4, 2009 I only want to run the function on load, as it will display the messages to start with.. instead of them displaying for everything when someone edits a field as it doesn't look too good. It's your call, but I would reate my server-side validation to output the page with the same error messages/states that the JavaScipt validation would do. Then you don't need to run the JS validation onload. Again, if you are running server-side validation to catch errors for users w/o JS, what would those users see in the way of error messages if you are relying on the JS to diplay the errors? Quote Link to comment Share on other sites More sharing options...
smithmr8 Posted March 4, 2009 Author Share Posted March 4, 2009 Well, seeing as the form is only submitted when the JS returns a true value. If JS wasn't enabled and the form was submitted incorrectly, it would return to the form, with the fields still containing the submitted values and the error messages displaying where the JS ones do, except this is server side. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 4, 2009 Share Posted March 4, 2009 Well, seeing as the form is only submitted when the JS returns a true value. If the user does not have JS enabled, the form will always be submitted. The form can't check if a true or false value is returned if JS is not enabled. f JS wasn't enabled and the form was submitted incorrectly, it would return to the form, with the fields still containing the submitted values and the error messages displaying where the JS ones do, except this is server side. Not sure I follow the last bit. Are you saying the server-side code is generating error messages to be displayed? If so, I would agree that is a good process. Based upon your request it appeared you wanted the JS to run on load of the page to display the error messages, which wouldn't make sense if the form was submitted with errors because the user did not have JS enabled. Again, it's your call. I'm just giving my two cents. Good luck. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 5, 2009 Share Posted March 5, 2009 http://www.roscripts.com/Ajax_form_validation-152.html Quote Link to comment Share on other sites More sharing options...
smithmr8 Posted March 5, 2009 Author Share Posted March 5, 2009 I understand the form would be submitted regardless when JS is disabled. This is exactly the reason I have server side validation to do the same thing as my JS validation, except without the lovely dynamic front-end. I just want to have JS as it makes things look so much more professional, which is exactly the look I'm going for. Quote Link to comment Share on other sites More sharing options...
smithmr8 Posted March 5, 2009 Author Share Posted March 5, 2009 Thanks darkfreaks, but my one is alot more simplistic, which suits my JS coding ability. . Cheers anyway. Quote Link to comment Share on other sites More sharing options...
kickstart Posted March 5, 2009 Share Posted March 5, 2009 I don't suppose there is a way to get the name of the current page you are looking at is there ? So I can only put that in the body tag when its on that page. Think that comes down to the design of your site. For example on a phpBB based site I was building I set up a link to a pages javascript as a template field in the header, and only populated on those pages that required it, just ignoring it on most other pages even though they used the same template. Nothing to stop the javascript file from using window.onload (or variations on it to allow multiple onload functions). All the best Keith Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.