Jquery validation is a powerful data verification plug-in that supports "validation"
Rule is the validation rule. The rule controls or restricts the input controls in the form. For example, "This item is required" and "this item cannot be less than n characters ", or "This is not a valid Email Address"
And so on. Most of these rules are similar to Asp.net validation controls. Unfortunately, the validation control of Asp.net itself cannot work in the MVC framework, because the server-side control is subject to
Viewstate restrictions, while the MVC Framework does not have the viewstate feature ......
A rule can be applied to the input box in two ways:
1. Set the class attribute in the input box.
Plain
Text HTML:
-
<InputName = "email"
Id = "email" maxlength = "60"
Class = "Required
Email "type =" text "/>
You can see that the class attribute in the tag is set to "required" and "email", which indicates that the input box is mandatory and the rules bound to the content of a valid email string. Multiple rules must be separated by a space character.
2. Specify a script for the rule.
Plain
Text Javascript:
- <Script
Type ="Text/JavaScript">
- $(Document).Ready(Function(){
- $("# Form-sign-up").Validate(
{
- Rules:
{
- Email:
{
- Required:
True,
- Email:
True
- },
- Messages:
{
- Email:
{
- Required:
"Please provide an email",
- Email:
"Please provide a valid email"
- } });
- });
- </SCRIPT>
Specify the rules for the input box whose ID is "email" in the form container whose ID is "form-sign-up, the prompt information displayed when user input verification fails is also set. These prompts can be customized in the "messages" section of the script. The prompt message is optional, jquery
The validation plug-in provides a set of predefined prompts. If you want to improve the user experience, we recommend that you set more friendly prompts.
Finally, a more interesting and important rule method is "remote", that is, "remote Rules ", it can also be called "server-side Verification" (The above verification is completed on the client browser ). Verification is performed on the server. It is important to verify whether the user name exists. Remote verification is executed in Ajax form. You can use the MVC Controller method.
Plain
Text Javascript:
- <Script
Type ="Text/JavaScript">
- $(Document).Ready(Function(){
- $("# Form-sign-up").Validate(
{
- Rules:
{
- Login:
{
- Required:
True,
- Remote:
'<% = URL. Action ("isloginavailable", "accounts ")
%>'
- }
- },
- Messages:
{
- Login:
{
- Required:
"Please provide an alias",
- Remote:
Jquery.Format("{0} is already in
Use")
- }
- } });
- });
- </SCRIPT>
The only requirement on the server controller side is that the JSON result returns the verification result. It is easy to implement in the MVC framework:
Plain
Text C #:
- PublicJsonresult isloginavailable(String
Login)
- {
- Jsonresult
Result =NewJsonresult();
- If (Login =
"Boho")
- Result.Data=False;
- Else
- Result.Data=True;
- ReturnResult;
- }
In the above processing, if the input data in the input box is "boho", the verification fails and the user will see an error message "boho is already in use"
Error message style:
Plain
Text CSS:
- Label. Error{
- Display:Block;
- Color:Red;
- Font-style:Italic;
- Font-weight:Normal;
- }
- Input. Error{
- Border: 2pxSolid Red;
- }
- TD. FieldInput. error, TD. FieldSelect. error, tr. ErrorrowTD. Field
Input, tr. ErrorrowTD. FieldSelect{
- Border: 2pxSolid Red;
- Background-color:
# Ffffd5;
- Margin: 0px;
- Color:Red;
-
}
You can download the sample code here.