Click here to Skip to main content
15,893,622 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am validating the username from server side,but i am unable to validate. The checkuser method is not getting called.

What I have tried:

C#
public JsonResult Checkuser(string userdata)
       {
           System.Threading.Thread.Sleep(200);
           var search = db.Test1.Where(x => x.name == userdata).FirstOrDefault();
           if(search!=null)
           {
            return Json(1);
           }
           else
           {
            return Json(0);
           }
       }


HTML
<script>
    function Usercheck()
    {
        $("#Status").html("Checking....");
        $.post("@Url.Action("Checkuser","Default2")",
            {
                userdata: $("#name").val()
            },
            function (data)
            {
                if (data == 0)
                {
                    $("#Status").html('<font color="Green"> Available ! </font>');
                    $("#name").css("border-color", "Green");

                }
                else
                {
                    $("#Status").html('<font color="Red"> User already exists</font> ');
                    $("#name").css("border-color", "Red");
                }
            });
    }
</script>
Posted
Updated 18-Nov-18 5:18am
Comments
F-ES Sitecore 24-Oct-18 4:31am    
return something like

return Json(new {success=true});

and

return Json(new {success=false});

then in the js check

if (data.success)
Richard Deeming 25-Oct-18 14:09pm    
Where is your client-side Usercheck method called from?

Not sure how you are calling the above Method (UserCheck)

You should use remote validations in MVC to make it work.

Implementing Remote Validation in MVC[^]

Another link which may help

Remote validation in MVC 5,To Check UserName and MailId is Already exist or not [^]

This is much simpler than what you have done, you would have to add "jquery.validate" and "jquery.validate.unobtrusive" to make Remote Validations work.
 
Share this answer
 
Try with this
$.post("@Url.Content("~/ControllerName/Checkuser")", { userdata: "Default2" }, function (data) {
 if (data != "") {
         alert("Ok")
 }
});



public JsonResult Checkuser(string userdata)
       {
           Object ret=0;
           System.Threading.Thread.Sleep(200);
           var search = db.Test1.Where(x => x.name == userdata).FirstOrDefault();
           if(search!=null)
           {
             ret=1;
             return Json(ret, JsonRequestBehavior.AllowGet);
           }
           else
           {
             return Json(ret, JsonRequestBehavior.AllowGet);
           }
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900