Mvc-model data Annotations (iii) a consideration for-remote validation

Source: Internet
Author: User

First of all, in general, the validation of a property may require more than one remote authentication, for example, for the user name, we need to make some restrictions on its length, which can be solved by the stringlength feature, and also need to verify that the user name cannot be duplicated. This needs to be solved by the remote feature.

Now you need to add another validation, that is, the user name cannot contain forbidden words. The forbidden word is saved in the database. At first I think this is simple, and then add a remote verification does not solve the originalThe same attribute is not supported with two remote authentication。 What to do at this time, try to write two verification in the same remote, but this can only show a errormessage, is rejected.        (I thought the method of the remote attribute could only return a bool value and not return errormessage). In fact remote verification can also customize ErrorMessage, I suddenly realized that I had always thought that I could only return true or FalseOf If you do not return a bool value when validating to False, you can return errormessage directly. Here's how:
        Public Jsonresult Checkusername (StringUserName) {if (Isuniquename (userName) && Isforbiddenname (userName)) {return Json (true, Jsonrequestbehavior.allowget); } else if (! Isuniquename (userName) {return Json ( " user name is not unique!  "else {return Json ( Span style= "color: #800000;" > " username does not contain forbidden words!  "   

This will perfectly solve the problem of requiring multiple remote authentication.

Mvc-model data Annotations (iii) a consideration for-remote validation

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.