After putting breakpoints in JS code, I got that my .blur function is being called 2 times. How to resolve this?
Now put a breakpoint at the beginning of your $(document).ready(......
function to see if it is executed twice. if it is executed twice then it attaches two handlers to the blur event....otherwise it means that the blur event is caused twice...let me know which of the two options is true.
Maybe the ajax call confuse someway the event handling. Instead of calling directly the update inside the on blur try put the call into a function, say: myAjaxCall. Then instead of calling directly this function do:
setTimeout(myAjaxCall, 0);
This way the js engine first finish completely the event handling and then start the ajax call. hope this will work.
agarwal.peeu...
Member
12 Points
32 Posts
jQuery validation for Email
Apr 17, 2012 02:50 PM|LINK
Hi all,
I've written code for validation of Email editor as following:
View code: <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/SAMS.js")" type="text/javascript"></script> @using (Html.BeginForm("RegisterStudent", "Register")) { @Html.ValidationSummary(true) <br /> <div class="account"> <h3>Student-Account</h3> <table> <tr> <td><p><label>@Html.LabelFor(model => model.Email) :</label> @Html.EditorFor(model => model.Email)<br /> <span style="margin: 0 0 0 40px;">@Html.ValidationMessageFor(model => model.Email)</span><br /> <span class="display">Enter a valid e-mail address</span></p></td> . . . . }in SAMS.js:
$(document).ready(function () { $("#Email").blur(function () { //alert("Hello"); email = $("#Email").val(); //alert(email); $.ajax( { type: "POST", url: "/Register/ValidateEmail", data: "{\"email\":\"" + email + "\"}", dataType: "json", contentType: "application/json", success: function (response) { //alert(response); if (response == false) { $(".display").show(); $(".display").text("Email address is not valid."); $(".display").css("color", "red"); } else { $.ajax( { type: "POST", url: "/Register/CheckUserAvailability", data: "{\"email\":\"" + email + "\"}", dataType: "json", contentType: "application/json", success: function (response) { if (response == true) { $(".display").show(); $(".display").text("User with this email already exists."); $(".display").css("color", "red"); } else { $(".display").text(""); $(".display").hide(); } } }); } } }); }); })In Controller:
[HttpPost] [AllowAnonymous] public JsonResult ValidateEmail(string email) { if (!email.Contains("@")) { return Json(false); } else if (email.IndexOf('@') != email.LastIndexOf('@')) { return Json(false); } else { string[] parts = email.Split('@'); if (!parts[1].Contains(".")) { return Json(false); } return Json(true); } } [HttpPost] [AllowAnonymous] public JsonResult CheckUserAvailability(string email) { return Json(_repository.CheckUserAvailability(email)); }But when I'm debugging this: ValidateEmail() and CheckUserAvailability() are being executed 2 times for each entry in Email Editor.
Kindly suggest how to resolve this?
francesco ab...
All-Star
20912 Points
3279 Posts
Re: jQuery validation for Email
Apr 17, 2012 03:16 PM|LINK
Why don't use the RemoteAttribute to do your verifications?
Anyway...I have not read in detail your code but there are mainly two possibilities:
1) the blur event is caused twice for some reason (maybe after the ajax call the focus remain in the textbox..so anothe blur is caused
2) Your javascript code is executed twice, so two blur event handlers are added
Put breakpoints in your javascript code to discover what happened
Mvc Controls Toolkit | Data Moving Plug-in Videos
agarwal.peeu...
Member
12 Points
32 Posts
Re: jQuery validation for Email
Apr 17, 2012 03:22 PM|LINK
As I'm new to MVC3, how to use RemoteAttribute for validation?
It would be helpful if you can give me some link on that.
After putting breakpoints in JS code, I got that my .blur function is being called 2 times. How to resolve this?
Thanks in advance
francesco ab...
All-Star
20912 Points
3279 Posts
Re: jQuery validation for Email
Apr 17, 2012 08:47 PM|LINK
just google it ....google is full of examples...see this for instance
http://www.tugberkugurlu.com/archive/check-instantly-if-username-exists-asp-net-mvc-remote-validation
Now put a breakpoint at the beginning of your $(document).ready(......
function to see if it is executed twice. if it is executed twice then it attaches two handlers to the blur event....otherwise it means that the blur event is caused twice...let me know which of the two options is true.
Mvc Controls Toolkit | Data Moving Plug-in Videos
agarwal.peeu...
Member
12 Points
32 Posts
Re: jQuery validation for Email
Apr 18, 2012 04:30 AM|LINK
I put breakpoint there, but it is being called just once. So I guess Blur event is caused twice.
Please guide me how to resolve this?
francesco ab...
All-Star
20912 Points
3279 Posts
Re: jQuery validation for Email
Apr 18, 2012 09:32 AM|LINK
Maybe the ajax call confuse someway the event handling. Instead of calling directly the update inside the on blur try put the call into a function, say: myAjaxCall. Then instead of calling directly this function do:
setTimeout(myAjaxCall, 0);
This way the js engine first finish completely the event handling and then start the ajax call. hope this will work.
Mvc Controls Toolkit | Data Moving Plug-in Videos