I have created a form using struts tags. I have submitted the form asynchronously using struts jQuery's submit tag. Now before I submit my form i want to call a JavaScript function checkUser()
, that checks if there is some client side error And prevents the form from submitting. The form with the JavaScript function is as follows:
<script type="text/javascript">
function loadXMLDoc() {
var xmlhttp;
var k = document.getElementById("register_userDetails_username").value;
var urls = "pass.jsp?ver=" + k;
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4) {
document.getElementById("err1").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("GET", urls, true);
xmlhttp.send();
}
</script>
<script type="text/javascript">
function checkuser() {
alert("hi");
var i = document.getElementById('err1').innerHTML.search("Unavailable");
alert(i);
var j= document.getElementById('err1').innerHTML.search("Min length:8");
alert(j);
if (i!= -1 || j!=-1) {
alert("hello");
return false;
}
alert("false");
return false;
}
</script>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sj" uri="/struts-jquery-tags"%>
<jsp:include page="menubar.jsp"></jsp:include>
<s:form action="register" method="post" theme="css_xhtml" cssClass="form1" style="margin:8px 211px" onsubmit="return checkuser()">
<div class="searchHeader">Sign Up</div>
<div id="passwordMatchError">
<s:property value="error"/>
</div>
<s:textfield label="Name" name="userDetails.name" maxlength="30"/>
<s:textfield label="Username" name="userDetails.username" onkeyup="loadXMLDoc()" maxlength="30">
<div id="err1"></div>
</s:textfield>
<s:password label="Password" name="userDetails.password" maxlength="45"/>
<s:hidden name="userDetails.role" value='U' />
<s:password name="confirmPassword" label="Confirm Password" maxlength="45"/>
<s:radio label="Sex" name="userDetails.sex" list="#{'M':'Male','F':'Female'}" />
<s:textfield label="Phone No" name="userDetails.phoneno" maxlength="10"/>
<s:textfield label="E-mail" name="userDetails.email" maxlength="45"/>
<div id="submitdivid">
<sj:submit value="SUBMIT" cssClass="orangebuttonsmall" targets="mainContent" id="sas" />
</div>
</s:form>
Now when I enter some value in username field that already exist in db, then it shows string Unavailable
in div having id as err
.
On onsubmit
event of form , I have called checkuser
method. But with sj:submit
, onsubmit
event is not working. Can anyone help me with how to call make this onsubmit
work My aim is to prevent form from submitting, if there exists the String Unavailable
or Min Length:8
in div err
. Any alternative solutions are also accepted.