srkirkland / DataAnnotationsExtensions

Validation extensions for DataAnnotations, including optional MVC client validation implementations
https://dataannotationsextensions.apphb.com/
BSD 3-Clause "New" or "Revised" License
308 stars 86 forks source link

validator fires when first letter is entered #52

Closed plkilroy closed 11 years ago

plkilroy commented 11 years ago

Hi Ive installed:

AppStart folder: RegisterClientValidationExtensions.cs

using DataAnnotationsExtensions.ClientValidation;

[assembly: WebActivator.PreApplicationStartMethod(typeof(MvcWURFL.ASPX.App_Start.RegisterClientValidationExtensions), "Start")]

namespace MvcWURFL.ASPX.App_Start { public static class RegisterClientValidationExtensions { public static void Start() { DataAnnotationsModelValidatorProviderExtensions.RegisterValidationExtensions();
} } }

Model:

[Required(ErrorMessage="Email Required")]

[RegularExpression(@"^((([a-z]|\d|[!#\$%&'+-\/=\?^_`{|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(.([a-z]|\d|[!#\$%&'+-\/=\?^`{|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+))|((\x22)((((\x20|\x09)(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))(((\x20|\x09)(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))).)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|.||~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))).?$", ErrorMessage = "_Valid Email Required")]

    [DataType(DataType.EmailAddress)]
    public string Email { get; set; }

MasterPage.aspx:

<%= Scripts.Render("~/bundles/jquery") %> <%= Scripts.Render("~/bundles/jqueryval")%>

Sorry about all the code but thought to provide as much info as possible!

The issue is the Email Expression (*Valid Email Required) validator fires when first letter is entered, clientside not post back. All other validation working correctly.

Thought to ask the Master, many thanks Paul

srkirkland commented 11 years ago

In the code above, you aren't using DataAnnotationsExtensions at all, just the [Required] and [RegularExpression] validators that come in the MVC box. If I'm missing something, let me know.

You might want to have a look at the jquery validation documentation for specifics on when to fire the validators if you don't like the default behavior.

plkilroy commented 11 years ago

Thanks Scott,

I am still having an issue clientside, first letter entered in email txt field fires the validator, not a valid email.

Can I firstly check I have things set up correctly!?

Im on .Net4 so using NuGet I installed both DataAnnotationsExtensions + MVC

This created in Appstart folder RegisterClientValidationExtensions.cs

In my Master file I reference, bundles/jquery + bundles/jqueryval and in web.config ClientValidationEnabled + UnobtrusiveJavaScriptEnabled value =true.

I think im good to go

I have looked at your blog, INTRODUCING DATA ANNOTATIONS EXTENSIONS and have followed the example.

Below my model

ContactViewModel

using System.ComponentModel.DataAnnotations; using DataAnnotationsExtensions;

[Required] [Email] public string Email { get; set; }

On http://dataannotationsextensions.org the email sample works how it should, so ? is what am i missing.

Much appreciate your help, Paul

srkirkland commented 11 years ago

If you visit the plugin site at http://docs.jquery.com/Plugins/Validation, it appears that this behavior is now the default, validating after one character. The http://dataannotationsextensions.org/ site is using jquery.validate 1.9, while 1.10.0 was released recently and may have changed the validation behavior.

So AFAIK, this is a change in the jquery validation plugin functionality. They may have a setting available to change back to the old behavior but i haven't looked in to it.

plkilroy commented 11 years ago

Thanks Scott for taking the time to work that out, great info. Although not the best new behaviour from JQuery, a custom email rule maybe a work around. Cheers P


From: Scott Kirkland notifications@github.com To: srkirkland/DataAnnotationsExtensions DataAnnotationsExtensions@noreply.github.com Cc: plkilroy plkilroy@yahoo.com Sent: Wednesday, November 28, 2012 11:12 AM Subject: Re: [DataAnnotationsExtensions] validator fires when first letter is entered (#52)

If you visit the plugin site at http://docs.jquery.com/Plugins/Validation, it appears that this behavior is now the default, validating after one character. The http://dataannotationsextensions.org/ site is using jquery.validate 1.9, while 1.10.0 was released recently and may have changed the validation behavior. So AFAIK, this is a change in the jquery validation plugin functionality. They may have a setting available to change back to the old behavior but i haven't looked in to it. — Reply to this email directly or view it on GitHub.