File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes validate form fields using javascript in struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "validate form fields using javascript in struts" Watch "validate form fields using javascript in struts" New topic
Author

validate form fields using javascript in struts

amrut sabade
Ranch Hand

Joined: Aug 16, 2010
Posts: 90
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

Joined: Aug 03, 2008
Posts: 9302
    
  17

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)...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
amrut sabade
Ranch Hand

Joined: Aug 16, 2010
Posts: 90
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Aug 16, 2010
Posts: 90
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Aug 16, 2010
Posts: 90
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

Joined: Sep 29, 2008
Posts: 12617

No, it's not correct.

Moving to HTML/JavaScript forum; this isn't really Struts-related.
amrut sabade
Ranch Hand

Joined: Aug 16, 2010
Posts: 90
kk
amrut sabade
Ranch Hand

Joined: Aug 16, 2010
Posts: 90
so can you tell me how i can cal function on submit click
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Aug 16, 2010
Posts: 90
can you please tell me what is the logic i should apply for checking name dosent contains any special character
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Code tags, not quote tags.
amrut sabade
Ranch Hand

Joined: Aug 16, 2010
Posts: 90
ok.. this is my frist time so didnt know abt it can help me now?
Tom Varga
Greenhorn

Joined: Aug 09, 2010
Posts: 8
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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Aug 16, 2010
Posts: 90
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

Joined: Sep 29, 2008
Posts: 12617

I already told you how to do that.
amrut sabade
Ranch Hand

Joined: Aug 16, 2010
Posts: 90
i have following code

which calls:-

how how can i cal function Check and submit on the submit onclick
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Aug 09, 2010
Posts: 8
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

Joined: Sep 29, 2008
Posts: 12617

But it's not necessarily true that every submit button will require validation, or the same validation.
amrut sabade
Ranch Hand

Joined: Aug 16, 2010
Posts: 90
thank you very much sir.. finally i have completed assignment ...without your help it was not possible...
thanks a lot..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: validate form fields using javascript in struts