MVC + jquery validate for user input verification
MVC Server:
1. Verify user input in controller. If verification fails, run
Modelstate. addmodelerror ("loginname", resource. loginname + resource. wordspace + resource. cannotbeblank );
2. Place
<% = Html. validationsummary () %>
JS client:
1. Import the corresponding JS File
<SCRIPT src = "/JS/jquery-1.4.2.js" type = "text/JavaScript"> </SCRIPT>
<SCRIPT src = "/JS/jquery. Validate. js" type = "text/JavaScript"> </SCRIPT>
2. Place
<Label id = "MessageBox"> </label>
3. Add the following JS Code at the bottom of the page. Code
$ (Function (){
$ ("# Form1"). Validate ({
Rules :{
Loginname: {required: True, RegEx: "^ [0-9] + $ "},
Password: {required: true}
},
Messages :{
Loginname: "<% = resource. loginname + resource. wordspace + resource. cannotbeblank %>"
},
Errorlabelcontainer: "# MessageBox ",
Wrapper: "Li"
});
});
The preceding code has been bilingual.
Other common verification methods include:
Required, remote, minlength, maxlength, rangelength, Min, Max, range, email, URL, date, dateiso, number, digits, creditcard, accept, belong to, etc.
See:
Http://docs.jquery.com/Plugins/Validation/validate