You are on page 1of 21



 
g  
  

 
 
    





 

c 
 

 

 

     


 
  
 
 
 
  
   !"    

 #$
 
 
%

  


  
 & 
 ' 
 
$( 
  )
 
 
 
 
 
 

 #
 *  +
, 
 c( !
 -+
' 
 

 & 
(.
  '
/ 01
 -+
' 
 

 $
 
 


     

 
 2!! 
2' 
  34' 
 
& #
 $
 
 


  

 
 $
 
 
( 0 
5 
46.7
 0 

 ' 
 
( !8 (

 #
5 
46. & c %
 $
 
 
5
5 
46.' 
 
%



 

 

c 
›  
¬ 
9  -   ! .  
 / (¬
 ¬
 
 2(.    ¬
  ( :/ !! .    
¬
    9 -  


2 ¬
    9  -  :.    
 
  ( ! 
  
 !( ! 2 
9  -  :. /
   ! 
 
 
   

 
  
 / 
 
 
2    
 :
 (
 1
. /
    ! 
 
 . 
 
   
  !!  1

  

/ 
 + &   /

    /  ! 
 
( !  

2    1
:. /
 *  +
   

  
   ! 
 
:
! 
  !
/    
 
  
 

  :. /

!!    ! -
 
 

+c;02' 
  342 ( :
( !
 1  
 
  
 /
  ( 
 
 

( !
 .:. /
 +c;0&'#5 
46.
 
:1
  
 
(


 (
 1
 
 
/
 46.
 
 

› 
2 
9  -3  :. /
   / 

G     ! 


 
 
 1   
 
  


  (  

G $   
 
 
 
 
 
!  1
 

G 2!! +c;02' 
  34 1  
 
  

G 2  5 


46.
 .  
 
 
 



~  
   
º !   (  /

!   !
  
G +c;0 +c;0&'#
G '
 +
01 

G +6+"   <01 



   =

c #
¬ º  
  
  . !>
!  .
 8c ( !2  
#    !
 ( !(/
?
2"@)7#%




~ 
m  
  
,  
 :!  (  . /
! 
  
 
 
  '
 +
 

  
   
    (



à  

9  -3  
 !
. (  /
1
 
 01
 
  !  
 

 01
 
 
 
 
 

# 01
# 5
2' 
  34  ! 
 

 01
 5
5 
46. 
 




0 
! 
!  !
  3 

¬ 0  1

  !
.  (  

  
 
.  

 ( !
 1
 º   
 
  

(.   

 
/ 
    1
 




¬ ~
01
 
  !$ ' 
 
5
' 
 

 


à  
  
     


c 
2 
1
:. /
   /     ! 
 
.
&'#' 
 

 
 
,   :. /

   
 

 + & 1 ! 
 
   
 (  !    
 

 ! "


2 
 :. /
   
 
 
   
0   
  
  
  / 

( !  "
 3  
 
    + A01-++
  !
' 
 
AB

    /(     4 :/
 /

    ! 
#   /C 
    (   !
   "
 3   "   (   
#   $   !   
%
using System;
using System.Collections.Generic;
using System.Web;
using System.ComponentModel.DataAnnotations;

 2 
   " ( !  "

.:.   
 "&! 
 
(
 
:/
 

    (
  
 
 
%
using System;
using System.Collections.Generic;
using System.Web;
using System.ComponentModel.DataAnnotations;

oAttributeUsage(AttributeTargets.Field | AttributeTargets.Property,
AllowMultiple = false, Inherited = true)]
publicclassPriceValidationAttribute : ValidationAttribute
{
}


¬ 
5 !
  /  ! 
 D  
   

oSystem.AttributeUsage(System.AttributeTargets.All,
AllowMultiple = false,Inherited = true)]

c 
"
!
  ! 
(
    
 
!   /
 

 
#"2

  System.AttributeTargets.All:   
(.  
!!<( 1 !: :c .:&  ,
=

 ' 2(

  :    
  
!   
 
 

()2(

  (  :    

 

.
 
  

º   !   "&!
!  


7 "(
    (   !
   
:  (  /
   
   
< +
+c;0&'#  !' 
 
01  !$ ' 
 
   -
+ =
%
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.ComponentModel.DataAnnotations;

namespaceMvcMusicStore.Validations
{
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,
AllowMultiple = false, Inherited = true)]
publicclassPriceValidationAttribute : ValidationAttribute
{
privatedecimal minPrice = 0.01M;
privatedecimal maxPrice = 100.00M;
}
}


¬     ( * 





E : . 
( !
   >.  8+
 :! 
  
 
! !




 2!!   



E  ! ( ( !   
 
>
( 
  ! 
 
  
 

c 7
< +
+c;0&'#  !' 
 
01  !$ ' 
 
2 ' 
-
+ =
%
using System;
using System.Collections.Generic;
usingSystem.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,
AllowMultiple = false, Inherited = true)]
publicclassPriceValidationAttribute : ValidationAttribute
{
privatedecimal minPrice = 0.01M;
privatedecimal maxPrice = 100.00M;

public PriceValidationAttribute(): base("The price is not in a valid range")


{
}

publicoverridebool IsValid(object value)


{
decimal price = (decimal)value;
if (price <this.minPrice || price >this.maxPrice)
returnfalse;
returntrue;
}
}



+  !)  $  


2 
 :. /
   /    
   "  /
  ! 
 .
 
  
 
! 4   
 3  "(
     
    !  
  " 
%
using MvcMusicStore.Validations;

# $      
/
     
 
%
[Range(0.01, 100.00, ErrorMessage = "Price must be between 0.01 and 100.00")]
oPriceValidation]

c 

(  ! : "(
  
 (  /
 
%
namespace MvcMusicStore.Models
{
[MetadataType(typeof(AlbumMetaData))]
publicpartialclassAlbum
{
// Validation rules for the Album class
...
[Required(ErrorMessage = "Price is required")]
[PriceValidation]
publicobject Price { get; set; }
}
}



, -!) 
2 
 :. /
     ! '
/!  
   

  !
 
 

 c     
 

   4   
 9 ! B / J  !J  
/
 
(

 ( ! 
(. .  
  
 
!  

c %
!
Validating an album price



¬ ~
01
 
 
 
 
 


à  
    
 
2  
 1
.     !  
 
! (   !

  


2 
1
:. /
   /   
 
 
 
,   . /

!! 
&'#(  "2 (  
 

/

!   1
 :   
! /
  (  
  !

  !    -. !


¬     -
 +c;0&'   
     
( 

  
 
      / 2    
 : . 
 


( !
 


2 
 :. /
   /& 
 ' 
 
$ 
 -!  -

 /
 
   ! 
 

 3  
 
    + A01-
 +
  !
' 
 
AB

    /C (

   ( :  !

 -!  -
# 3   -!  -   (   
#/"  !   
%
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Mvc;

c )

 2 
   -!  - ( !    - 
%
using System;
usingSystem.Collections.Generic;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Mvc;

namespaceMvcMusicStore.Validations
{
publicclassModelClientPriceRangeValidationRule: ModelClientValidationRule
{
}
}

    /   (   -!  -     
 
 /
 .   !  ! 
< +
+c;0&'#
 
 
 
 
01  -+ =
%
usingSystem.Web.Mvc;

namespaceMvcMusicStore.Validations
{
publicclassModelClientPriceRangeValidationRule : ModelClientValidationRule
{
publicModelClientPriceRangeValidationRule(stringerrorMessage, decimalminPrice,
decimalmaxPrice)
{
ErrorMessage = errorMessage;

ValidationType = "priceOnRange";

ValidationParameters.Add("minPrice", minPrice);
ValidationParameters.Add("maxPrice", maxPrice);
}
}
}


¬ 2  (  /
  . /
  /


 /
     





c 

+ !" 
2 
 :. /
  !    "  ( ! 
 
 
 -

 3   "( !     
&    " 
!!  (  "
( 
%
publicclassPriceValidationAttribute: ValidationAttribute,IClientValidatable

 &    " 
 ! 0   -
< +
+c;0&'#
 
 
 
 
01F
 ' 
 
$ 

-+ =
%
...
publicclassPriceValidationAttribute : ValidationAttribute, IClientValidatable
{
...
publicoverrideboolIsValid(object value)
{
...
}

JJ Client-Sidevalidation
publicIEnumerable<ModelClientValidationRule>GetClientValidationRules(ModelMeta
datametadata, ControllerContextcontext)
{
var rule = newModelClientPriceRangeValidationRule("The price is not in a valid
range.", this.minPrice, this.maxPrice);

yieldreturn rule;
}
...



, !1  .  2  


    /*  +
(
      !
   3
 3   3 
 (  /

 
  /
 
  

 º /
  
   ! 
  ( !  (
  "  
 
 1
 
1
Sys.Mvc.ValidatorRegistry.validators["priceOnRange"] = function(rule) {
varminPrice = rule.ValidationParameters.minPrice;

c 
varmaxPrice = rule.ValidationParameters.maxPrice;
var message = rule.ErrorMessage;

return function (value, context) {


if (value >maxPrice || value <minPrice) {
return false;
}
return true;
};
};



3  .#!)  '  * 2  


 3  *
/( ! '  !
 (    (  /
 *  +
(
  /
   (4 1(  :&'
 
 
    !
 
 
;1: 9&
  
 


4
-
<h2>Create</h2>

<scriptsrc="JScriptsJMicrosoftAjax.js"type="textJjavascript"><Jscript>
<scriptsrc="JScriptsJMicrosoftMvcValidation.js"type="textJjavascript"><Jscript
>
<scriptsrc="JScriptsJPriceValidation.js"type="textJjavascript"><Jscript>
<%Html.EnableClientValidation(); %>
<%using (Html.BeginForm()) {%>
-



  -!) 
2 
 :. /
     ! 
/! ( !    
 
 

 
/        !

 c     
 

   4   
 9 ! B / J  !J  
 (
  
  
 (  
 
 G:Hº /
  (  /
 !  

c 

!+
Validating an album price at client side



¬ ~
01
# 5
2' 
  34  ! 
 



à    ›


  
2 
1
:. /
   / 
!!    ! -
 
 

+c;0
( "5"32 ( 2  
 1
 . / /
  
 
 
  ( !
  ! 
 
 (   
 ; /. /
 ( "5"3   

 (
  ! :( !
 1  
 
  
 /
  ( 
 
 


( !

 (!( "5"3   ) 

c #
 3  
 
&&
+      *6,2    
( "5"37!
 3  "(
   
!! ( "5"3
(  
%
-
namespaceMvcMusicStore.Models
{
[MetadataType(typeof(AlbumMetaData))]
publicpartialclassAlbum: IValidatableObject
{
-


¬ ( "5"3
( <( !+. ! !  & "    
= 
 
/ . 
 
   42  !!: & :/
 !
 
( 
4 
!!   
( 
 
 
IEnumerable<ValidationResult>Validate(ValidationContextvalidationContext)

º   !      *


!  


# 2!! ( "5"3 !  "  ! /
 
( 

 . !/
   !
  

       ' 
 
$  4
< +
+c;0&'#
2' 
  34 
 
01#' 
 ! -
+ =
%
publicIEnumerable<ValidationResult>Validate(ValidationContextvalidationContext
)
{
MusicStoreEntitiesstoreDB = newMusicStoreEntities();

if (storeDB.Albums.Any(a =>a.Title.Trim().ToUpper() ==
this.Title.Trim().ToUpper() &&a.ArtistId == (int)this.ArtistId))
yieldreturnnewValidationResult("Existing Album", newstringo] { "Title" });
}


¬   -  ! <I01



 !J=   (

/
   


<I
J=º /
     ! 

 . 1 
(

  ! 

!! 
 &   :. !
  ( !(  /
 
 
  ( 
 


 ! ,   :   ( 

!!  ! 
 !! 

c 



+ -!) 
2 
 :. /
.     !    
 

 c     
 

   4   
 9 ! B / J  !J 
# ,
 ( !/
  (  /
  I0J
 
(
 I4#89:( 
  :/
 . 
  
   (
 
 c   !
 ( !º /
  !  *!"  
  ( 

 


!,
Validating the insertion of an existing album



¬ ~
01
 5
5 
46. 
 



c 
à      
  
2 
1
:. /
   /  &'#5 
46. 
 
 
 
,  
 . /

46.
 
 
  & c    
46.  .

¬  5 


46.    - 4 1(
1*  +
 
  
!     
   

.!




  
 

 -!) ".  "!& "3;#


2 
 :. /
   
 
( 

46.
   !    
 

!  
 3  
 
    + A01-5 
*  +

' 
 
AB
       
 

   4   
 9 ! B / J  !J  
/
 
(

 ( ! 
(. .  
 
!  


!3
lient validation disabled

c 7

# 2   / :    
/   

4  )
-
<formaction="/StoreManager/Create"method="post">
<divclass="validation-summary-errors"><ul><listyle="display:none"><Jli>
</ul></div>

-
<p>
<labelfor="Album_Title">Title</label>
<inputclass="input-validation-
error"id="Album_Title"name="Album.Title"type="text"value=""/>
<spanclass="field-validation-error">An Album Title is required<Jspan>
</p>
<p>
<labelfor="Album_Price">Price</label>
<inputclass="input-validation-
error"id="Album_Price"name="Album.Price"type="text"value="0"/>
<spanclass="field-validation-error">Price must be between 0.01 and
100.00</span>
-



+  "!   . /" .!


2 
 :. /
  
46. ( !/" .!(
:/
 
.( 
 (  
  /+c;0&'# 4 
 3 /" .!(
  4 : !        ". 

  
< 42/" .!
-
<configuration>
<appSettings>
<addkey="ClientValidationEnabled"value="true"/>
<addkey="UnobtrusiveJavaScriptEnabled"value="true"/>
</appSettings>
-


¬ º     


  
 
.  F   1    !  
HtmlHelper.ClientValidationEnabled = true;

c 


.:.  
 
 ' 
 
0  

  .     
  ! 





, !& "3;#  !


2 
 :. /
   
46.(  
  & c 
 3    ')
 &'# 
(   3;# 
< 4
-
<headrunat="server">
<linkhref="/Content/Site.css"rel="Stylesheet"type="text/css"/>
<title><asp:ContentPlaceHolderID="TitleContent"runat="server"/></title>
<scriptsrc="..J..JScriptsJjquery-1.4.1.js"type="textJjavascript"><Jscript>
<scriptsrc="..J..JScriptsJjquery.validate.js"type="textJjavascript"><Jscript>
<scriptsrc="..J..JScriptsJjquery.validate.unobtrusive.js"type="textJjavascript"><J
script>
</head>

¬   46.
 
  

 &'# / 4 º  (
! 

 
 
  4D ( J




3 -!) &!& "3;#  


2 
 :. /
     ! 
/! ( ! 
 
 
 


46.
 
 /        / !
 c      
 

   4   
 9 ! B / J  !J  
/
 
(

 ( ! 
(. .  
 
!  

c %

! 
lient validation with jQueryenabled

# 2   / :     (  
/ 

4
-
</title>
<scriptsrc="..J..JScriptsJjquery-1.4.1.js"type="textJjavascript"><Jscript>
<scriptsrc="..J..JScriptsJjquery.validate.js"type="textJjavascript"><Jscript>
<scriptsrc="..J..JScriptsJjquery.validate.unobtrusive.js"type="textJjavascript"><J
script>
</head>
-
<p>
<labelfor="Album_Title">Title</label>
<inputdata-val="true"data-val-length="The field Title must be a string with a
maximum length of 160."data-val-length-max="160"data-val-required="An Album Title
is required"id="Album_Title"name="Album.Title"type="text"value=""J>

</p>
<p>
<labelfor="Album_Price">Price</label>
<inputdata-val="true"data-val-number="The field Price must be a number."data-
val-range="Price must be between 0.01 and 100.00"data-val-range-max="100"data-

c )
val-range-min="0.01"data-val-required="Price is
required"id="Album_Price"name="Album.Price"type="text"value="0"J>
<spanclass="field-validation-valid"data-valmsg-for="Album.Price"data-valmsg-
replace="true"></span>
</p>

<spanclass="field-validation-valid"data-valmsg-for="Album.Title"data-valmsg-
replace="true"></span>
</p>
-

¬ ,   


  
 
:5 
46.    
/
   - -
rulename@KmessageKB /
 
 (   5 
46.
 
   !

(
 ( !
  
 

G $2
G $22"
G $22!
G $22!2J$22!2*
G $22=
G $22!)
G $22!)2*J$22!)2

      (
/
 ! $    :   
 /   

   
 


, 1 !:c
 
  (  /
    

 !  
oRequired(ErrorMessage = "Price is required")]
oRange(0.01, 100.00, ErrorMessage = "Price must be between 0.01 and
100.00")]
public object Price { get; set; }


(
5 
46.:    

<input data-val="true"

data-val-number="The field Price must be a number."


data-val-range="Price must be between 0.01 and 100.00"

c 
data-val-range-max="100"

data-val-range-min="0.01"
data-val-required="Price is required"
id="Album_Price" name="Album.Price" type="text" value="0" />




¬ ~
+!! .


 
B. !

9  -3  .     /    ! 
 

.  /
  
  ( (  /
  
G "    
( !  
 


G +c;0&'#2
 ' 
  
( 

G   !&'& 


 ' 
 
$
G &'' 
 



 

G   !  
 

2' 
  34
( 

G +c;0&'#5 
46. 
 



c