ASP. net mvc exploration path-added string length range validation attribute

Source: Internet
Author: User
Dataannotations provides requiredattriations for null, empty, and stringlengthattribute for String Length verification. It is strange that a stringlengthrangeattribute verification is not provided. For example, when verifying input, the password may be in the range of 6-20. At this time, we can expand a validationattribute, which is simple: [Attributeusage (attributetargets. Field | Attributetargets. Property
, Allowmultiple =   False , Inherited =   True )]
Public   Class Stringlengthrangeattribute: validationattribute
{
Private   Const   String _ Defaulterrormessage =   " The length of '{0}' must be between {1}-{2} characters. " ;

Public Stringlengthrangeattribute ( Int Minlength, Int Maxlength)
: Base (_ Defaulterrormessage)
{
If (Minlength <   0 )
Throw   New Argumentoutofrangeexception ( " Minlength " , Minlength
, " The minimum length of a string cannot be less than 0. " );
If (Maxlength <   0 )
Throw   New Argumentoutofrangeexception ( " Maxlength " , Maxlength
, " The maximum length of a string cannot be less than 0. " );
If (Maxlength <= Minlength)
Throw   New Argumentoutofrangeexception ( " Maxlength " , Maxlength
, " The maximum length of a string must be greater than the minimum length. " );
Minlength = Minlength;
Maxlength = Maxlength;
}

Public   Override   Bool Isvalid ( Object Value)
{
String Valueasstring = Value As   String ;
If (String. isnullorempty (valueasstring )) Return   True ;
Return Valueasstring. Length > = Minlength
&& Valueasstring. Length <= Maxlength;
}
Public   Override   String Formaterrormessage ( String Name)
{
Return String. Format (cultureinfo. currentuiculture, errormessagestring,
Name, minlength, maxlength );
}
Public   Int Maxlength
{
Get ;
Private   Set ;
}

Public IntMinlength
{
Get;
Private Set;
}
}

Example:

Public   Class Userinputedit
{
[Stringlengthrange ( 6 , 20 , Errormessage =   " The logon password must be between 6 and 20 characters. " )]
[Displayname ( " Logon Password " )]
Public   String Password { Get ; Private   Set ;}

}< br>

Note :. net 4, system. componentmodel. the stringlengthattribute in the dataannotations namespace adds the minimumlength attribute to set the minimum String Length.

Related Article

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.