Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I done with following code.</p> <p><strong>Model class:-</strong></p> <pre><code> [FormatValidtion("Value", "Format", "MinLength", "MaxLength")] public class Sample { #region ModelProperties public string Id { get; set; } [Required(ErrorMessage = "Please Enter Value")] public string Value { get; set; } public string Format { get; set; } public string MinLength { get; set; } public string MaxLength { get; set; } #endregion } </code></pre> <p><strong>Custom Validation Class:-</strong></p> <pre><code>[AttributeUsage(AttributeTargets.Class)] public class FormatValidtion : ValidationAttribute { public string Value { get; set; } public string Format { get; set; } public string MinLength { get; set; } public string MaxLength { get; set; } public string Format1 { get; set; } public string Value1 { get { return Value; } } public FormatValidtion(String value, String format, String minLength, String maxLength) { Value = value; Format = format; MinLength = minLength; MaxLength = maxLength; } public override Boolean IsValid(Object value) { Type objectType = value.GetType(); PropertyInfo[] neededProperties = objectType.GetProperties() .Where(p =&gt; p.Name == Value || p.Name == Format || p.Name == MinLength || p.Name == MaxLength) .ToArray(); if (neededProperties.Count() != 4) { throw new ApplicationException("PropertiesMatchAttribute error on " + objectType.Name); } Boolean isMatched = true; this.Format1 = Convert.ToString(neededProperties[1].GetValue(value, null)); switch (Convert.ToString(neededProperties[1].GetValue(value, null))) { case "Alpha": Regex regExAlpha = new Regex(@"/^[A-Za-z]+$/"); if (!regExAlpha.IsMatch(Convert.ToString(neededProperties[0].GetValue(value, null)))) { isMatched = IsValidLength(Convert.ToString(neededProperties[0].GetValue(value, null)).Length, Convert.ToInt32(neededProperties[2].GetValue(value, null)), Convert.ToInt32(neededProperties[3].GetValue(value, null))); } break; case "Number": Regex regExNumber = new Regex(@"/^[0-9]+$/"); if (!regExNumber.IsMatch(Convert.ToString(neededProperties[0].GetValue(value, null)))) { isMatched = IsValidLength(Convert.ToString(neededProperties[0].GetValue(value, null)).Length, Convert.ToInt32(neededProperties[2].GetValue(value, null)), Convert.ToInt32(neededProperties[3].GetValue(value, null))); } break; case "AlphaNum": Regex regExAlphaNum = new Regex(@"/^[a-zA-Z][a-zA-Z0-9]+$/"); if (!regExAlphaNum.IsMatch(Convert.ToString(neededProperties[0].GetValue(value, null)))) { isMatched = IsValidLength(Convert.ToString(neededProperties[0].GetValue(value, null)).Length, Convert.ToInt32(neededProperties[2].GetValue(value, null)), Convert.ToInt32(neededProperties[3].GetValue(value, null))); } break; default: isMatched = false; break; } return isMatched; } /// range validation public bool IsValidLength(int valueLenght, int minLenght, int maxLenght) { if (valueLenght &gt;= minLenght &amp;&amp; valueLenght &lt;= maxLenght) return true; return false; } } </code></pre> <p><strong>Validation Adapter Class:-</strong></p> <pre><code>public class FormatValidtionAdapter : DataAnnotationsModelValidator&lt;FormatValidtion&gt; { public FormatValidtionAdapter(ModelMetadata metadata, ControllerContext context, FormatValidtion attribute) : base(metadata, context, attribute) { } // To Provide Error messages dynamically depends on format. public override IEnumerable&lt;ModelValidationResult&gt; Validate(object container) { string ErrorMessage = null; if (!Attribute.IsValid(Metadata.Model)) { switch (Attribute.Format1) { case "Alpha": ErrorMessage = "Please enter alphabets only"; break; case "Number": ErrorMessage = "Please enter numbers only"; break; case "AlphaNum": ErrorMessage = "Please enter alphanumeric only"; break; default: break; } yield return new ModelValidationResult { Message = ErrorMessage, MemberName = Attribute.Value1 }; } } } </code></pre> <p><strong>Global.aspx.cs:-</strong></p> <pre><code> protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterRoutes(RouteTable.Routes); DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(FormatValidtion), typeof(FormatValidtionAdapter)); } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload