I am relatively new to ajax and I am trying validate email and mobile through response body check mobile email in my controller, I am unable to spot my mistake, since ajax syntax is new to me, can anyone help me out in right direction? Thankyou
$('#email','#mobile').blur(function(){
alert("in validation");
var enteredEmail = $("#email").val();
var enteredMobile = $("#mobile").val();
$.ajax({
url : window.location "checkmobileemail",
data : {email:enteredEmail , mobile:enteredMobile},
success : function(result) {
if (result == 'Duplicate') {
$("#emailMsg","#mobileMsg").html("Email or Mobile already exists");
$("#email","#mobile").focus();
$("#addButton","#saveChanges").prop("disabled",true);
} else {
$("#emailMsg","#mobileMsg").html("");
$().prop("disabled",false);
}
}
});
Controller :
@RequestMapping(value="/checkmobileemail",method= RequestMethod.GET)
@ResponseBody
public String checkMobileEmail(HttpServletRequest req, Model model) {
String mobile = req.getParameter("mobile");
String email = req.getParameter("email");
String id = req.getParameter("id");
System.err.println("id " id "mobile: " mobile " , email: " email);
return service.findByEmailAndMobile(email,mobile);
}
Service method:
@Override
public String findByEmailAndMobile(String email, String mobile) {
Customer customer = repository.findByEmailAndMobile(email,mobile);
return (customer == null)? "Unique" : "Duplicate";
}
CodePudding user response:
Use this with class attribute. Multiple id will not work with the version you are using it seems.
for ex:-
$(document).ready(function(){ $(".test").change(function(){ Your code and use change method. Do not use blur. }); });
HTML=>
It will work. Thanks!
