I've follow the steps in the Internet to set up the validation for the password.
I'm using ASP.NET MVC4+razor+jquery, but the validation seems not work.Here are my codes:
CSHTML:
@using (Html.BeginForm("Index","Home",FormMethod.Post,new { id="resetForm"}))
{
<ol class="round">
<li class="one">
<h5>Select the Application.</h5>
@model PWDManagementCenter.Models.UserApplicationModels
@Html.DropDownListFor(x => x.selectedUserApp, Model.userApps)
</li>
<li class="two">
<h5>Input Login ID of Application</h5>
<input name="txtLogonId" />
</li>
<li class="three">
<h5>Input the new password</h5>
<input id="txtPwd" name="txtPwd" type="password" />
<h6>Retype the password</h6>
<input id="txtPwd2" name="txtPwd2" type="password" /><p />
<input id="btnSubmit" type="submit" value="Save" />
</li>
</ol>
}
JS in html:
<script src="/Scripts/jquery-1.7.1.js" type="text/javascript" />
<script src="/Scripts/jquery.validate.js" type="text/javascript" />
<script type="text/javascript">
$(document).ready(function () {
$('#resetForm').validate({
rules: {
txtPwd: {
require: true, minlength: 6, maxlength: 20
},
txtPwd2: {
require: true, equalTo: '#txtPwd', minlength: 6, maxlegth: 20
}
},
messages: {
txtPwd: {
require: "Password is required!",
minlength: "Password contains at least 6 characters",
maxlength: "Password contains at most 20 characters"
},
txtPwd2: {
equalTo: "Make sure input the same password as above"
}
}
});
</script>
And the controller:
[HttpPost]
public ActionResult Index(UserApplicationModels model)
{
string appName = model.selectedUserApp;
string id = Request.Form["txtLogonId"];
string newPwd = Request.Form["txtPwd"];
string newPwd2 = Request.Form["txtPwd2"];
......
}
When I click the submit button in debug mode, it jump to this Index function without validation.
Why? Anything I missed? Please help.
I follow this simple validation demo
Thanks to those who answered the question. You gave me the hits to solve the problem.
The script tab should end with "
</script>" rather than "/>" So this "<script src="/Scripts/jquery.validate.js" type="text/javascript" />" can't work but this "<script src="/Scripts/jquery.validate.js" type="text/javascript" ></script>" worksAfter 1, I got an runtime error, saying that "jscript runtime error object doesn't support this property or method" This is because we include jquery twice.
After 1,2 , validation is normal.