Reputation: 209
I am using struts2-jquery-plugin-3.2.1 . I am using submit button of it, i am calling javascript validation on onclick event but my form is submitting even though validation not done.Here is code:
<sj:submit
value="Add"
targets="idRightMainDiv"
button="true"
buttonIcon="ui-icon-gear"
formIds="addExerDetailsForm"
onclick="return SetsRepsFormValidate()" //calling javascript validation
tabindex="7"/>
function SetsRepsFormValidate() {
var summary = "";
summary += isSets();
summary += isReps();
if (summary != "") {
dhtmlx.alert(summary);
return false;
}
else {
return true;
}
}
Upvotes: 1
Views: 4179
Reputation: 209
I got solution in this link this:
put this code in head section-
<sj:head jqueryui="true"/>
<script type="text/javascript">
$.subscribe('before', function(event,data) {
var fData = event.originalEvent.formData;
alert('About to submit: \n\n' + fData[0].value + ' to target '+event.originalEvent.options.target+' with timeout '+event.originalEvent.options.timeout );
var form = event.originalEvent.form[0];
if (form.echo.value.length < 2) {
alert('Please enter a value with min 2 characters');
// Cancel Submit comes with 1.8.0
event.originalEvent.options.submit = false;
}
});
$.subscribe('complete', function(event,data) {
alert('status: ' + event.originalEvent.status + '\n\nresponseText: \n' + event.originalEvent.request.responseText +
'\n\nThe output div should have already been updated with the responseText.');
});
$.subscribe('errorState', function(event,data) {
alert('status: ' + event.originalEvent.status + '\n\nrequest status: ' +event.originalEvent.request.status);
});
</script>
And wrote struts 2 jquery submit button's events-
<div class="type-button">
<sj:submit targets="result"
value="AJAX Submit"
timeout="2500"
indicator="indicator"
onBeforeTopics="before"
onCompleteTopics="complete"
onErrorTopics="errorState"
effect="highlight"
effectOptions="{ color : '#222222' }"
effectDuration="3000"/>
</div>`
Upvotes: 1
Reputation: 250882
You should really add validation to the onsubmit
event of the form as the form can also be submitted by hitting the enter key.
Your current validation will only fire if the user clicks on the button.
Upvotes: 0