MVC Annotation Validation

Source: Internet
Author: User

Front:

@{
Layout = null;
}
@using System.Activities.Expressions
@model MvcApplication1.Models.News
<! DOCTYPE html>

<meta name= "viewport" content= "Width=device-width"/>
<title>MyIndex</title>
<script src= "~/scripts/jquery-1.7.1.min.js" ></script>
<script src= "~/scripts/jquery-ui-1.8.20.min.js" ></script>
<script src= "~/scripts/jquery.validate.min.js" ></script>
<script src= "~/scripts/jquery.validate.unobtrusive.min.js" ></script>
@* <link href= "~/content/themes/base/jquery.ui.datepicker.css" rel= "stylesheet"/>*@
<link href= "~/content/themes/base/jquery.ui.all.css" rel= "stylesheet"/>
@* <link href= "~/content/themes/base/jquery-ui.css" rel= "stylesheet"/>*@
<style type= "Text/css" >

</style>
<script type= "Text/javascript" >
$ (function () {
$ ("#SubDateTime"). DatePicker ();
$ ("#Title +span"). CSS ("Color", "red");
$ ("#No +span"). CSS ("Color", "red");
JQuery.validator.addMethod ("Enforcetrue", function (value, element, param) {
return value! = "";
//});
JQuery.validator.unobtrusive.adapters.addBool ("Enforcetrue");

});
</script>
<body>
@using (Html.BeginForm ("Myindex", "Home", FormMethod.Post))
{
@Html. AntiForgeryToken ()
@Html. ValidationSummary (True)
<div>
<span> Press No. </span> @Html. textboxfor (x = x.no) @Html. Validationmessagefor (x=>x.no)
<span> News Headlines </span> @Html. textboxfor (x = x.title) @Html. Validationmessagefor (X=>x.title)
@Html. textboxfor (x=>x.subdatetime,new{@class = "DatePicker"})
<input type= "Submit" value= "Submission"/>
</div>
}
</body>

Model

Using System;
Using System.ComponentModel.DataAnnotations;
Using System.Linq;
Using System.Web;
Using Microsoft.Ajax.Utilities;

Namespace Mvcapplication1.models
{
public class News
{
[Required]
[Iscellphone (errormessage = "{0} is not in the correct format")]
[Display (Name = "mobile number")]
public string Title {get; set;}
[Required]
public string No {get; set;}
Public DateTime subdatetime {get; set;}
}
}

Validation Attribute class:

Using System;
Using System.Collections.Generic;
Using System.ComponentModel.DataAnnotations;
Using System.Text.RegularExpressions;
Using SYSTEM.WEB.MVC;

Namespace Mvcapplication1.models
{
<summary>
Verify Phone number format
</summary>
[AttributeUsage (Attributetargets.property | Attributetargets.field | Attributetargets.parameter, AllowMultiple = False)]
public class Iscellphoneattribute:validationattribute
{
Public Iscellphoneattribute ()
: Base ("{0} format is incorrect! ")//mark1
{
}

<summary>
overriding validation methods
</summary>
<param name= "Value" ></param>
<param name= "Validationcontext" ></param>
<returns></returns>
protected override Validationresult IsValid (object value, Validationcontext validationcontext)
{
if (value! = null)
{
var valueasstring = value. ToString ();
Const string Regpattern = @ "^ (\+?[ 0-9]{2,4}\-[0-9]{3,4}\-) | ([0-9]{3,4}\-))? ([0-9]{7,8}) (\-[0-9]+)? $|^[1][3,5,8][0-9]{9}$ ";
@ "^1[3|4|5|8][0-9]\d{4,8}$";
//@"^((\+? [0-9] {2,4}\-[0-9]{3,4}\-) | ([0-9]{3,4}\-))? ([0-9]{7,8}) (\-[0-9]+)? $|^[1][3,5,8][0-9]{9}$ ";
if (! Regex.IsMatch (valueasstring, Regpattern))
{
var errormessage = formaterrormessage (validationcontext.displayname);//the {0} placeholder that formats the validation information at MARK1 displays the name of the property, If you set display (name= "mobile phone number") then the "mobile number" is displayed.
var errormessage = formaterrormessage (valueasstring);//If you want to display the number instead of the field name, change the parameter to the mobile phone number passed over.
return new Validationresult (errormessage);
}
}
return validationresult.success;
}
}

}

MVC Annotation 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.