I created a simple login page on mvc where the user must be notified when not entering password or username, using data annotation as following.
[Required(ErrorMessage = "password needed")]
public string password { get; set; }
[Required(ErrorMessage = "user name needed")]
public string username{ get; set; }
In the controller I'm testing with hard coded values as follwoing:
public ActionResult Login2(Login login)
{
string username = login.username;
string password = login.password;
if (ModelState.IsValid)
{
if (username == "100000" && password == "password")
{
return RedirectToAction("Index", "Home");
}
else{
return View();
}
}
return View();
In the viwe, I verify with validation message as below:
<label class="form-label" for="contactPhone">
username
</label>
@Html.EditorFor(model => model.CVR, new {
htmlAttributes = new { @class = "form-input", PlaceHolder =
"username", maxlength = "10", Type="HtmlInputFile",
id="username" , value=""} })
<br>
@Html.ValidationMessageFor(model =>
model.username, "", new { @class = "alert alert-error" })
<label class="form-label" for="contactPhone">
p<ssword
</label>
@Html.EditorFor(model => model.password,
"Password", new { htmlAttributes = new { @class = "form-
input", PlaceHolder = "password" } })
<br>
@Html.ValidationMessageFor(model =>
model.password, "", new { @class = "alert alert-error" })
<button class="button button-primary button-active"
id="danger" type="submit">Login</button>
The problem is that the alert box is displayed on the page load (empty). Once the submit button without filling the fields the error message is displayed. My questions are as follwoing: 1- how to hide the red alert box when loading the login page? 2- how to add more restrections for validating the username and password. for example:
- how to validate the correct password? and display the error message in the alert box?
- how to validate the correct username?and display the error in the alert box?