• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

validate form fields using javascript in struts

 
amrut sabade
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi everyone
i have simple form having elements like name,address, and email , i have to validate them for e.g name must not contain special character.... how can i do this in struts using java script... please help me...
 
Ankit Garg
Sheriff
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you just want to apply normal javascript validation or apply struts validations through javascript on the client side?? In Struts 2 you can apply Ajax validations as given here (I'm not sure if this method is currently supported, a lot of things have been deprecated like Ajax theme and I think David mentioned even Dojo plugin is depricated)...
 
amrut sabade
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're relying on the user to validate their own form?! That's crazy. And I hope you're not relying solely on client-side validation, since it's trivially bypassable.
 
amrut sabade
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok means i want it soud alert user about bad input on submition of form.. and my mentor told me particually use the java script function..so i am trying that ..can we do it in that way...
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, you can validate using JavaScript.

But relying on the user to click "validate" before submitting the form seems like a Really Bad Idea.

And regardless, you still can't ignore server-side validation.
 
amrut sabade
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok thanks actually i have submit button and i have called javascript function onclick even... so the function i written isValid is correct? and can i can cal it on submit button click?n how?
am new to struts n this forun also so thanks for replying..really
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it's not correct.

Moving to HTML/JavaScript forum; this isn't really Struts-related.
 
amrut sabade
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kk
 
amrut sabade
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so can you tell me how i can cal function on submit click
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please UseCodeTags when posting code or configuration. Unformatted code and configuration is unnecessarily difficult to read. You can edit your post by using the button.
 
amrut sabade
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please tell me what is the logic i should apply for checking name dosent contains any special character
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Code tags, not quote tags.
 
amrut sabade
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok.. this is my frist time so didnt know abt it can help me now?
 
Tom Varga
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope the following example will help you:



For further details and examples, see the following pages:
onsubmit event,
submit method.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You call a validate method onclick the same way you'd call a submit method. If you return a boolean false from the validate method, and return the results of the validate method in your onclick, the form won't be submitted. So onclick="return validate(this);" or however you're going to call it.

For finding special characters I'd just use a JavaScript regular expression.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Varga wrote:I hope the following example will help you:

Note that as a user I really don't appreciate receiving my validation messages one at a time: better to accumulate them and give them to me all at once. Also not a fan of pop-up validation messages, because then they go away when the dialog is gone and I no longer have the requirements available to me unless they're already on the page.
 
amrut sabade
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you for such a nice example sir ... now i can write script i have one question to you sir.. i have one for function that called on submit buttion for calling action class now how can i cal this function too
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I already told you how to do that.
 
amrut sabade
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have following code

which calls:-

how how can i cal function Check and submit on the submit onclick
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Like I said: call the validate statement, return boolean false from it if validation fails, and return that value from your onclick attribute.
 
Tom Varga
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not use the onclick event of the submit button for form validation, use only the onsubmit event of the form. The onsubmit event is fired when the user clicks on a submit button in the form ( a form may have more than one submit button).
The onsubmit event is cancelable, if it is canceled, the form will not be sent. If the event handler for the onsubmit event returns false, the event is canceled.

You no need to call the submit method of your form, if you use the onsubmit event for form validation. Simply return true, if the form is valid and return false if it isn't.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But it's not necessarily true that every submit button will require validation, or the same validation.
 
amrut sabade
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much sir.. finally i have completed assignment ...without your help it was not possible...
thanks a lot..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic