add username and password validation
This commit is contained in:
@ -16,6 +16,73 @@ namespace WebApplication1.Controllers
|
|||||||
{
|
{
|
||||||
private newswebappEntities db = new newswebappEntities();
|
private newswebappEntities db = new newswebappEntities();
|
||||||
|
|
||||||
|
private void ValidatePassword(string password, string confirmPassword)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(password))
|
||||||
|
{
|
||||||
|
ModelState.AddModelError("", "Password cannot be empty");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (password != confirmPassword)
|
||||||
|
{
|
||||||
|
ModelState.AddModelError("", "Passwords don't match");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (password.Length < 8)
|
||||||
|
{
|
||||||
|
ModelState.AddModelError("", "Password must be at least 8 characters long");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!password.Any(char.IsUpper))
|
||||||
|
{
|
||||||
|
ModelState.AddModelError("", "Password must contain at least one uppercase letter");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!password.Any(char.IsLower))
|
||||||
|
{
|
||||||
|
ModelState.AddModelError("", "Password must contain at least one lowercase letter");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!password.Any(char.IsDigit))
|
||||||
|
{
|
||||||
|
ModelState.AddModelError("", "Password must contain at least one digit");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!password.Any(ch => !char.IsLetterOrDigit(ch)))
|
||||||
|
{
|
||||||
|
ModelState.AddModelError("", "Password must contain at least one special character");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ValidateUsername(string username)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(username))
|
||||||
|
{
|
||||||
|
ModelState.AddModelError("", "Username cannot be empty");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (username.Length < 3)
|
||||||
|
{
|
||||||
|
ModelState.AddModelError("", "Username must be at least 3 characters long");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (username.Length > 20)
|
||||||
|
{
|
||||||
|
ModelState.AddModelError("", "Username must not exceed 20 characters");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!username.All(char.IsLetterOrDigit))
|
||||||
|
{
|
||||||
|
ModelState.AddModelError("", "Username can only contain letters and digits");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// GET: users
|
// GET: users
|
||||||
public ActionResult Index()
|
public ActionResult Index()
|
||||||
{ if (Convert.ToInt32(Session["Userrole"]) > 0)
|
{ if (Convert.ToInt32(Session["Userrole"]) > 0)
|
||||||
@ -77,7 +144,8 @@ namespace WebApplication1.Controllers
|
|||||||
{
|
{
|
||||||
if (ModelState.IsValid)
|
if (ModelState.IsValid)
|
||||||
{
|
{
|
||||||
|
ValidatePassword(user.password, Request.Form["ConfirmPassword"]);
|
||||||
|
ValidateUsername(user.usename);
|
||||||
//check if the username exists
|
//check if the username exists
|
||||||
var check = db.users.FirstOrDefault(u => u.usename == user.usename);
|
var check = db.users.FirstOrDefault(u => u.usename == user.usename);
|
||||||
//checks username and password field in form
|
//checks username and password field in form
|
||||||
@ -110,11 +178,6 @@ namespace WebApplication1.Controllers
|
|||||||
return RedirectToAction("Index");
|
return RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
ModelState.AddModelError("",
|
|
||||||
"username and password field can not be empty");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return View(user);
|
return View(user);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -47,7 +47,7 @@
|
|||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
<label class="control-label col-md-2">Confirm password</label>
|
<label class="control-label col-md-2">Confirm password</label>
|
||||||
<input type="password" , id="ConfirmPassword", oninput="checkPasswords()" class="form-control" />
|
<input type="password" ,name="ConfirmPassword", id="ConfirmPassword", oninput="checkPasswords()" class="form-control" />
|
||||||
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
|
<span asp-validation-for="ConfirmPassword" class="text-danger"></span>
|
||||||
<span id="passwordMismatch" class="text-danger"></span>
|
<span id="passwordMismatch" class="text-danger"></span>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user