ASP. net mvc combined with jquery plug-in for data verification

Source: Internet
Author: User

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:

  1. <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:

    1. <Script
      Type ="Text/JavaScript">
    2. $(Document).Ready(Function(){
    3. $("# Form-sign-up").Validate(
      {
    4. Rules:
      {
    5. Email:
      {
    6. Required:
      True,
    7. Email:
      True
    8. },
    9. Messages:
      {
    10. Email:
      {
    11. Required:
      "Please provide an email",
    12. Email:
      "Please provide a valid email"
    13. } });
    14. });
    15. </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:

    1. <Script
      Type ="Text/JavaScript">
    2. $(Document).Ready(Function(){
    3. $("# Form-sign-up").Validate(
      {
    4. Rules:
      {
    5. Login:
      {
    6. Required:
      True,
    7. Remote:
      '<% = URL. Action ("isloginavailable", "accounts ")
      %>'
    8. }
    9. },
    10. Messages:
      {
    11. Login:
      {
    12. Required:
      "Please provide an alias",
    13. Remote:
      Jquery.Format("{0} is already in
      Use"
      )
    14. }
    15. } });
    16. });
    17. </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 #:

    1. PublicJsonresult isloginavailable(String
      Login)
    2. {
    3. Jsonresult
      Result =NewJsonresult();
    4. If (Login =
      "Boho")
    5. Result.Data=False;
    6. Else
    7. Result.Data=True;
    8. ReturnResult;
    9. }

    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:

    1. Label. Error{
    2. Display:Block;
    3. Color:Red;
    4. Font-style:Italic;
    5. Font-weight:Normal;
    6. }
    7. Input. Error{
    8. Border: 2pxSolid Red;
    9. }
    10. TD. FieldInput. error, TD. FieldSelect. error, tr. ErrorrowTD. Field
      Input, tr. ErrorrowTD. FieldSelect{
    11. Border: 2pxSolid Red;
    12. Background-color:
      # Ffffd5;
    13. Margin: 0px;
    14. Color:Red;
    15. }

      You can download the sample code here.

     

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.