locked
Check If UserName and Email Id Exists Asp mvc and c# RRS feed

  • Question

  • 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 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.

    • Moved by Fei Hu Friday, March 30, 2018 8:02 AM Asp.Net related
    Thursday, March 29, 2018 2:52 PM

All replies

  • In your C# code, 1 means “Username already exists”, but in Javascript it means “Username is not taken”. Maybe change some conditions.


    • Edited by Viorel_MVP Thursday, March 29, 2018 7:15 PM
    Thursday, March 29, 2018 7:15 PM
  • You can post to the MVC forum in ASP.NET forums.

    http://forums.asp.net/

    Thursday, March 29, 2018 11:55 PM
  • Hi Felipe1520,

    According to your question is more related to Asp.Net, you need to repost a new thread to Asp.Net forum for better support.

    The Visual C# discuss and ask the C# programming language, IDE, libraries, samples and tools. If you have some grammar or code errors, please feel free to contact us. We will try our best to give you a solution.

    Best Regards,

    Neil Hu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 30, 2018 8:01 AM
  • Ok, thank you!!
    Friday, March 30, 2018 5:31 PM