• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Validating forms

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Why two forms to being with? That's just odd right off the bat.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jimmy Wadia
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like Seetharaman Venkatasamy to comment on this. is that possible?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic