ASP.NET 4.5 Custom Validation Attribute. IsValid() called too late
<p>I am kind of a newbie in the ASP.NET world. I want to create a custom validation for a property of a viewModel class. Let's say that this validation checks if the input is an integer greater than 10. So in the MyViewModel.cs file we have the following code parts:</p>

<pre><code>[GreaterThanTen]
[RegularExpression("^[0-9][0-9]*$", ErrorMessageResourceType = typeof(Resources.Resource),
        ErrorMessageResourceName = "NonNegativeIntMessage")]
[Required(ErrorMessageResourceType = typeof(Resources.Resource),
        ErrorMessageResourceName = "RequiredValidationMessage")]
[UIHint("OwTextbox")]
public int MyInt { get; set; }
</code></pre>

<p>is the definition of the aforementioned property and:</p>

<pre><code>[AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class GreaterThanTenAttribute : ValidationAttribute
{
    public GreaterThanTenAttribute() : base(Resources.Resource.GreaterThanTen) { }

    protected override ValidationResult IsValid(Object value,ValidationContext validationContext)
    {
        if (value != null)
        {
            if (value is int) // check if it is a valid integer
            {
                int suppliedValue = (int)value;
                if (suppliedValue &gt; 10)
                {
                    return ValidationResult.Success;

                }
            }

        }

        return new ValidationResult(FormatErrorMessage(validationContext.DisplayName));
    }

}
</code></pre>

<p>is the extended ValidationAttribute class used to define the validation message.</p>

<p>Then there is a webpage which inludes this form:</p>

<pre><code>&lt;form name="my-form" id="my-form"&gt;
  &lt;div class="form-group"&gt;
     &lt;label for="MyObject_MyInt"&gt;My points:&lt;/label&gt;
     &lt;div class="form-group-inner"&gt;
         &lt;div class="field-outer"&gt;
            @Html.TextBoxFor(m =&gt; m.MyObject.MyInt, new { @class = "input-lg"})
            @Html.ValidationMessageFor(m =&gt; m.MyObject.MyInt)
         &lt;/div&gt;
      &lt;/div&gt;
   &lt;/div&gt;
&lt;/form&gt;
</code></pre>

<p>And when a button is clicked the following javascript function is called in order to validate the TextBox value and insert it into the database...</p>

<pre><code>function insertData() {
   if ($("#my-form").valid()) {
     ...
   }
}
</code></pre>

<p>The problem is that all standard validations such as the Regular Expression defining that the value should be a non-negative integer are performed when execution reaches the <code>if ($("#my-form").valid())</code> except for the custom <strong>IsGreaterThanTen</strong> validation which is called later on (not sure what triggers it) after the field has been validated and the value has been processed. What am I doing wrong?</p>