Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

check if username exists

$
0
0

hello,

I'm begginer and i need help!!

I need do validation in mvc core to check if username already exists.. 

My source code dont work. it returns 0 where it should be 1. That is, 0 would usually not exist and 1 user already exists.

      

I put it in controller:

public JsonResult CheckUsernameAvailability(string userdata)
        {
            System.Threading.Thread.Sleep(200);
            var SeachData = db.StudentDetails.Where(x => x.StuName == userdata).SingleOrDefault();
            if (SeachData != null)
            {
                return Json(1);
            }
            else{
                return Json(0);
            }

        }

View:

          <label class="control-label col-sm-2" for="User">Username:</label>
            <div class="col-sm-10">
                <input class="form-control" type="text" id="Username" name="Username" placeholder="Enter Username" onchange="UserCheck()">
            </div>
        </div>
        <div class="row">
            <label class="col-sm-2"></label>
            <div class="col-sm-10">
                <p id="Status" />
            </div>
        </div>
        <div class="row">
            <label class="control-label col-sm-2" for="Pwd">Password:</label>
            <div class="col-sm-10">
                <input class="form-control" type="Password" id="Password" name="Password" placeholder="Enter Password" onchange="UserCheck()">
            </div>
        </div>
    </div>
</div>
<script>
    function UserCheck() {
        $("#Status").html("Checking...");
        $.post("@Url.Action("CheckUsernameAvailability", "Home")",
            {
                userdata: $("#Username").val()
            },
        function (data) {
            if (data == 1) {
                $("#Status").html('<font color="Green">Available !. you can take it.</font>');
                $("#Username").css("border-color", "Green");

            }
            else {
                $("#Status").html('<font color="Red">That name is taken.Try Another.</font>');
                $("#Username").css("border-color", "Red");
            }
        });
    }
</script>

thanks.x


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>