2
Answers

Why can't I get my LogIn validation to work?

Photo of Marc Liljeqvist

Marc Liljeqvist

6y
642
1
Beginner here.
 
I can't seen to get my login validation to work. It is supposed to show that the login was succesful, or that the user information was invalid, but nothing happens.
Can anyone spot the problem? 

The form:  

  1. <div class="container" style="margin-top: 10%">    
  2. <div class="row">    
  3. <div class="col-md-4 col-md-offset-4 alert alert-info">    
  4. <h3 class="text-center">Login</h3>    
  5. <form id="loginForm">    
  6. <div id="msg"><ul style="color:red;">Invalid Email or Password</ul></div>    
  7. <div class="form-group">    
  8. <div class="input-group">    
  9. <span class="input-group-addon">    
  10. <i class="glyphicon glyphicon-envelope"></i>    
  11. </span>    
  12. <input class="form-control" type="email" name="email" id="email" placeholder="Email" />    
  13. </div>    
  14. </div>    
  15.     
  16. <div class="form-group">    
  17. <div class="input-group">    
  18. <span class="input-group-addon">    
  19. <i class="glyphicon glyphicon-lock"></i>    
  20. </span>    
  21. <input class="form-control" type="Password" name="pwd" id="pwd" placeholder="Password" />    
  22. </div>    
  23. </div>    
  24. </form>    
  25.     
  26. <div class="form-group">    
  27. <button class="btn btn-info form-control" type="submit" onclick="Login()">    
  28. <i class="glyphicon glyphicon-log-in"></i>    
  29. </button>    
  30. </div>    
  31.     
  32. <div class="form-group">    
  33. <a style="float:left">Forgot Password?</a>    
  34. <a style="float:right;cursor:pointer" onclick="SignUp()">Sign Up</a>    
  35. </div>    
  36.     
  37. </div>    
  38. </div>    
  39. </div>    
The script:  
  1. $("#msg").hide();  
  2.   
  3. var login = function () {  
  4. var data = $("#loginForm").serialize();  
  5. $.ajax({  
  6. type: "post",  
  7. url: "/Register/CheckValidUser",  
  8. data: data,  
  9. success: function (result) {  
  10. if (result == "Fail") {  
  11. $("#loginForm")[0].reset();  
  12. $("#msg").show();  
  13. }  
  14. else {  
  15. window.location.href = "/Register/AfterLogin";  
  16. $("#msg").hide();  
  17. }  
  18. }  
  19. })  
  20. }  
RegisterController: 
  1.  public JsonResult CheckValidUser(customer model)  
  2.         {  
  3.             string result = "fail";  
  4.             var DataItem = db.customer.Where(x => x.email == model.email && x.pwd == model.pwd).SingleOrDefault();  
  5.             if (DataItem != null)  
  6.             {  
  7.                 Session["customerID"] = DataItem.customerID.ToString();  
  8.                 Session["email"] = DataItem.email.ToString();  
  9.                 result = "Success";  
  10.             }  
  11.             return Json(result, JsonRequestBehavior.AllowGet);  
  12.         }  
  13.         public ActionResult AfterLogin()  
  14.         {  
  15.             if (Session["customerID"] != null)  
  16.             {  
  17.                 return View();  
  18.             }  
  19.             else  
  20.             {  
  21.                 return RedirectToAction("Index");  
  22.             }  
  23.         }  
  24.         public ActionResult Logout()  
  25.         {  
  26.             Session.Clear();  
  27.             Session.Abandon();  
  28.             return RedirectToAction("Index");  
  29.         }  
  30.     }  
  31. }  
AfterLogin:
  1. @{    
  2.     ViewBag.Title = "AfterLogin";    
  3. }    
  4.     
  5. <h2>AfterLogin</h2>    
  6.     
  7. @if (Session["costumerID"] != null)    
  8. {    
  9.     <div>    
  10.         Welcome : <a href="#">@Session["customerFirst"]</a><br />    
  11.         <a href="/Register/Logout">Logout</a>    
  12.     </div>    
  13. } 

Answers (2)