A calendar for a date type etc but browsers sometimes

This preview shows page 41 - 43 out of 87 pages.

a calendar for a date type, etc.) But browsers sometimes don't provide validation based on data types. So we often need to implement validation by adding other annotation classes. We'll talk about this in the coming slides. DisplayFormat Attribute Class The previous DataType allows you to specify what type of data to display, but it cannot specify the detailed format of displaying that type of data. The DisplayFormat class allows you to do this. It mainly accepts two parameters: o DataFormatString: a string specifying the format o ApplyFormatInEditMode: a bool value indicating whether the specified format should be applied to the input device in editing mode. This may not be supported by browser, but you can still try to have it. Example For instance, if you want to display date in "yyyy-mm-dd" format for the "ReleaseDate" property, you can do: In the "{0:yyyy-MM-dd}" above, the "0" means the format should be applied to the first variable. Since we only have one ReleaseDate variable here, it is not useful, but we still need to have it according to the syntax of 'format strings', which is discussed in the next slide. The "MM " means the month from 01 to 12. NB: don't use "mm" here, which means minutes. For how to code a format string, see o Custom numeric format strings: - us/dotnet/standard/base-types/customnumeric-format-strings o Custom Date and Time Format Strings: - us/dotnet/standard/base-types/customdate-and-time-format-strings Data Annotation – Validating data For Data Annotation on validating data, we'll talk about the following attribute classes: o Required o MaxLength and MinLength o StringLength o Range o EmailAddress o RegularExpression Required
This annotation specifies that the value for the HTML input device is required. For example: NB: The entire validation mechanism is already there for you (e.g., including a default error message). You only need to add the [Required] before a property If you want to customize the error message, you can do: And this ErrorMessage parameter is available to all the validation attribute classes. You only need to add the [Required] annotation to those properties that are of string type. o For the properties of other types, they are required by default in ASP.NET. Moreover, other validation annotations to be talked about in the coming slides won't automatically imply [Required]. o Actually, they'll all pass if a field is empty. o So if a field is of string type and required, you must add the [Required] annotation. MaxLength and MinLength These two specify the maximum length and minimum length of the string allowed in an input device respectively. For example, NB: you can put multiple attribute classes in one line and separate them by comma. StringLength This attribute class also allows you to specify the maximum length and minimum length of the string allowed in an input device.

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture