Skip to content

Instantly share code, notes, and snippets.

@0xh3xa
Last active April 4, 2018 14:06
Show Gist options
  • Select an option

  • Save 0xh3xa/6af1b38adcf88a9d19abe415af3022c9 to your computer and use it in GitHub Desktop.

Select an option

Save 0xh3xa/6af1b38adcf88a9d19abe415af3022c9 to your computer and use it in GitHub Desktop.
<html>
<head>
<script>
function hideErrorMsg(){
$("#errorTextFieldID").hide();
}
$(document).ready(function(){
hideErrorMsg();
});
function validate() {
var textField = $("#textFieldID");
$.ajax({
url: 'servlet_pattern_url',
type: "GET",
data: {
element: textField.val()
},
success: function (data) {
if(data.found){
textField.select();
$("#errorTextFieldID").show();
}
}
});
return false;
}
</script>
</head>
<body>
<form>
<input type="text" id="textFieldID" onblur="validate()" onfocus="hideErrorMsg()"/>
<span id="errorTextFieldID">enter another input, it's duplicated in db</span>
</form>
</body>
</html>
//How to make an Ajax request from html to servlet.
//NOTE: Found is a variable in Servlet:
public void doGet(HttpRequest request, HttpResponse response) throws Exception{
String element = request.getParameter("element");
//TODO check the element Does it found in db or not?
boolean found = db.check();
response.getWriter().write(found);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment