This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes Validating forms Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Validating forms" Watch "Validating forms" New topic
Author

Validating forms

Jimmy Wadia
Greenhorn

Joined: Nov 27, 2013
Posts: 10
In my search.jsp, I have created in javascript 'function validateForm()' which throws a few 'alert' messages if certain search criteria not met. The form for that is:
<form name="search" method="post" action="search.jsp" onsubmit="return validateForm()">

If user entered correctly, then I want the parameters in search.jsp to go to my servlet for further processing. The form for that is:
<form method="post" action="./servlet/search">

I want to use only one submit button, which is:
<input type=submit name=submit value= "Find Now">

Please help. Thanking you in anticipation.

JimmyW
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Welcome to the Ranch.

Why two forms to being with? That's just odd right off the bat.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

P.S. Remove the name=submit form your button! That's going to replace the native submit function and cause no end of trouble in the future.
Jimmy Wadia
Greenhorn

Joined: Nov 27, 2013
Posts: 10
Hi Bear,
Thanks for your welcome and suggestions.
Two forms because I would like to keep it as simple as possible. I could deploy a forward to a servlet or jsp, but I already have enough of those in my web application.

All that 'function validateForm()' does is check if both city and state fields have values etc. among other things. I could of course import the parameters into my servlet and do checking and then set attributes and retrieve the attributes in search.jsp, which is much more involved and not necessary if two form can be submitted easily.

I am intrigued by your suggestion to remove name=submit. It works well as of now. Just how it would replace the native submit function and cause problems, I would like to know.

Thanks.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Jimmy Wadia wrote:Two forms because I would like to keep it as simple as possible.

Not grokking that. Having two forms instead of one adds much complexity. Again, why two forms? There be something that hasn't been explained. How can two forms make things less complex than having one?

(By the way, avoiding forwards and the like simply because you "already have enough" is not a good reason to avoid something that might be the correct approach.)

I am intrigued by your suggestion to remove name=submit. It works well as of now. Just how it would replace the native submit function and cause problems, I would like to know.

Because it creates a property named submit on the form that occludes (better word than replace) the form's native submit() method. Never name anything "submit".
Jimmy Wadia
Greenhorn

Joined: Nov 27, 2013
Posts: 10
I agree that forwards etc are the correct methods, but I did see that Seetharaman Venkatasamy proposed a simple solution, and that really interests me if it can be done.

If someone can give me pointers (possibly with some code), that would be really appreciated.

Thanks for explaining the property concept for submit. I will implement that.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Not sure anyone can offer any advice without understanding the situation. Validating one form, but submitting another, is a really odd thing to do and you haven't really explained why you're heading down that path.
Jimmy Wadia
Greenhorn

Joined: Nov 27, 2013
Posts: 10
Bear Bibeault wrote:Not sure anyone can offer any advice without understanding the situation. Validating one form, but submitting another, is a really odd thing to do and you haven't really explained why you're heading down that path.


OK, my dilemma in detail: My 'search.jsp' is thrown by my servlet 'search.java' on app start to gather user input like name, city, state, zip etc. To properly construct the sql query, both city and state must be entered. To find within 'X' miles of a zip code, only the zip code must be entered and all other fields left blank.
So, in 'search.jsp' I have created 'function validateForm()' which throws 'alert' messages like "For Zip Distance Search enter only zip & leave other fields blank" upon checking user input after
submit button activated. In case 'function validateForm()' returns false, then these 'alert' messages will pop up. In order to achieve that, I have:
<form name="search" method="post" action="search.jsp" onsubmit="return validateForm()">

In the event the user enters all data correctly as required, then the parameters should go to my servlet 'search.java' for processing the sql query and forward same to 'searchresults.jsp' for
display. For that I have:
<form method="post" action="./servlet/search">

Both of these forms work, but of course you cannot have two forms in the same jsp with only one submit button. So I read a post in which Seetharaman Venkatasamy proposed a solution, which happens to be the earlier post, not this new topic post where my discussion has been moved to.

Hope I have explained in maximum detail what I am trying to achieve.

Happy Thanksgiving to one and all.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30057
    
149

This still seems like it could be done in one form to me. But if you still want to do this, you can. You use JavaScript to set the relevant values (zip code) in the second form and then form.submit() to submit it.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jimmy Wadia
Greenhorn

Joined: Nov 27, 2013
Posts: 10
Jeanne Boyarsky wrote:This still seems like it could be done in one form to me. But if you still want to do this, you can. You use JavaScript to set the relevant values (zip code) in the second form and then form.submit() to submit it.


Any kind of code sample to achieve same. Appreciate it.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30057
    
149

I don't think a code sample will help much since it dpeends on your code. But here's one.

form2.zip.value = form1.zip.value;
form2.submit();
Jimmy Wadia
Greenhorn

Joined: Nov 27, 2013
Posts: 10
I would like Seetharaman Venkatasamy to comment on this. is that possible?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

That is unlikely as it's been a while since he last posted. What beef do you have with Jeanne's approach?

That assumes, of course, that you have fixed your naming problem so that the submit() function hasn't been occluded as discussed previously.
Jimmy Wadia
Greenhorn

Joined: Nov 27, 2013
Posts: 10
I have fixed my naming problem, but that does not solve any issues. Of course, it will prevent inexplicable problems from happening somewhere in the future, as you explained.

I cannot understand Jeanne's solution. Where would those lines of code be? Inside a function, and what would they accomplish? After all, I have submitted by problem in exquisite detail, enough for someone to give me a coding solution like Seetharaman Venkatasamy did. Do you have his email? Would appreciate that.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

Jimmy Wadia wrote:After all, I have submitted by problem in exquisite detail, enough for someone to give me a coding solution

That's not exactly how things generally work here.

The code Jeanne gave you would be placed in the submit handler for the submitted form to copy a value from the first form's field to one in the other form, and then to submit the second form under script control.

But I've read your problem in detail and still see no reason whatsoever for all this complexity. In all of that you never explain why you feel you need a second form. What prevents you from validating and submitting the information in a single form?
Jimmy Wadia
Greenhorn

Joined: Nov 27, 2013
Posts: 10
But I've read your problem in detail and still see no reason whatsoever for all this complexity. In all of that you never explain why you feel you need a second form. What prevents you from validating and submitting the information in a single form?


What would your suggestion be in validating and submitting the information in a single form. Not actual code but pseudo code perhaps.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

The customary way to perform form validation is to establish a submit handler for the form. This handler gets called when the form is submitted. The script can check the form's values (or anything else it likes) and returns true to allow the form to proceed with the submission, or false to halt the submission. Displaying the errors on the page is a lot friendlier than alerts, but one step at a time.

Is there a re reason that this customary approach to the problem be unsuitable for your form?

(I'd also recommend using the jQuery JavaScript library for all of this as it makes everything much easier than in raw script.)
Jimmy Wadia
Greenhorn

Joined: Nov 27, 2013
Posts: 10
Bear Bibeault wrote:The customary way to perform form validation is to establish a submit handler for the form. This handler gets called when the form is submitted. The script can check the form's values (or anything else it likes) and returns true to allow the form to proceed with the submission, or false to halt the submission. Displaying the errors on the page is a lot friendlier than alerts, but one step at a time.


How do you create a submit handler that returns true/false and displays errors instead of alerts? Suggestions welcome.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60741
    
  65

At this point you should be writing code that we can start commenting on and refining. I'll get you started but for the rest you need to make an attempt on your own.

To establish a submit handler for a form with an id of thisIsMyForm using jQuery ('cause life is too short not to) could be accomplished with the following:


Jimmy Wadia
Greenhorn

Joined: Nov 27, 2013
Posts: 10
Hi All,
I finally succeeded in creating what I want. I do appreciate all your knowledgeable suggestions offered in good faith.

Even though I have not used any of your suggestions, which of course, are quite sound and which probably should have been implemented by me, the simple and elegant solution which I had in mind without going into any additional rigamarole has been finally implemented by me.

Check it out at: www.dentistreferralcenter.com

Enter a city without a state or enter any value in name field and choose zip distance search for 5 miles or more and see the error messages that pop up in another jsp to see what I am talking about.

Regards.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Validating forms
 
Similar Threads
When is the class that extends ActionForm executed
Allow only one Email in text box
Search in DATABASE with jsf
Http error status 405
form submit