Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Form validation help

Status
Not open for further replies.

techu4

Technical User
Jun 27, 2005
32
GB
Hi all,

I have a multipage form that uses divs that are hidden to create the different pages. I am using javascript to hide or display the different divs when a next button is pressed. What I also need to do is validate the data on the current div and to advise what is missing before displaying the next one. As far as i am aware you can only call 1 javascript function using the onclick command so I can't use javascript for validation. I was wondering if anyone can advise me on the best way to go for validating the fields before displaying the next div.

Hope this makes sense.

Regards

Techu4
 
Techu4 said:
As far as i am aware you can only call 1 javascript function using the onclick command so I can't use javascript for validation.
Incorrect. You can call as many functions as you want and separate them with a semi-colon. However, I do suggest you do most of the validation server-side once the page is submitted, since JS can never be fully trusted.
Code:
<button onclick="MyFunction1(); MyFunction2(); MyFunction3();">Click</button>
 
Hi Chris,

Thanks for that. I am not very familiar with javascript, coudl you advise me how to call one function from another.

Secondly. at the moment I am only checking wether a field has anything in it and not if what is in it is in the correct format or the correct type ie numerical or alpha.

Could you point me to any tuts you might know of so that I can learn how to validate these other things.

Thanks.
 
You CAN call multiple functions from the onclick command. You can actually write an entire javascript program in the onclick command if you want. Just separate lines with a semicolon. That being said, however, it is much simpler if you call a single function, and use that function to call others as needed. That makes it easier to maintain and easier to control.
Code:
function myClick() {
   if ( pageValid() ) {
      nextPage();
   } else {
      // error code here
   }
}

<... onClick="myClick();" ...>



Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Hello? Why am I completely ignored here?

Anyway. I think that calling two functions is logically better than calling one function within the other. Then you don't actually need to functions if one always executes the other. The whole point of using the functions is to call the code separately. One button might only validate, one only jump forward and one do both. If you call two functions with onclick, this is easily achieved, if you call one from another, it's entirely different story.

As for tutorial, try Google. Here's one page that helps.
 
Thanks everyone for your answers.

As stated I do not know much about js so I would appreciate any help with the actual script itself.

1. How would I validate that a field has something in it and that what is in it is of the correct format.

2. How do I determine whether the page is valid or not so I can then either move to the next page or display an error message requesting the user complete the missing fields.

Sorry to ask what might seem very basic questions, but as said I am new to all this.

Regards
 
However, it is unlikely that the OP would want to call the second funtion (to change pages) if the first function (to validate the page) finds an error, and simply chaining function calls together in the onclick is not going to help with that. And I do not recommend putting anything much more complicated that a simple function call into an onclick. Your code should stay in code sections where it belongs, not mixed up with the html.


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top