Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Validations fire on the first keystroke
Message
 
 
To
All
General information
Forum:
ASP.NET
Category:
MVC
Title:
Validations fire on the first keystroke
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
01560774
Message ID:
01560774
Views:
58
Hi everybody,

I am using modal dialog and the validations seem to fire immediately after I type the first digit in the textbox.

I am using the following Textbox.cshtml editor in the Shared/EditorTemplates
<div class="editor-label">
    @Html.Label((ViewData.ModelMetadata.DisplayName??ViewData.ModelMetadata.PropertyName),
        new Dictionary<string, object>
            {                
                { "for", ViewData.ModelMetadata.PropertyName }
            })
</div>

<div class="editor-field">
   @if (ViewData.ModelMetadata.ModelType.IsNumeric())
   {
    @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue, new { type = "number", @class = "numericOnly"  })      
   }
    else
    {
     @Html.TextBox("", (object)Model,
        new Dictionary<string, object>
            {
                { "id", ViewData.ModelMetadata.PropertyName },
                { "name", ViewData.ModelMetadata.PropertyName },
                { "class", "text-box single-line"},
                { "data-bind", "value: " + ViewData.ModelMetadata.PropertyName },
            })
    }
   
     @Html.ValidationMessage(ViewData.ModelMetadata.PropertyName,
        new Dictionary<string, object>
            {
                { "data-valmsg-for", ViewData.ModelMetadata.PropertyName }
            })
</div>
where numericOnly class is
$(".numericOnly").keypress(function (e) {
    if (String.fromCharCode(e.keyCode).match(/[^0-9]/g)) return false;
});
and I added this Remote attribute today
namespace CardNumbers.Models
{
    public class ClientViewModel
    {
        public Client Client { get; set; }

        [Required]
        [DisplayName("Client No")]
        [UIHint("Number")]
        [Column("client_no", TypeName = "smallint")]
        [Remote("doesClientNoExist", "Client", HttpMethod = "POST", ErrorMessage = "Client Number already exists. Please enter a different Client Number.")]
        public virtual Int16 Number { get; set; }

        [Required]
        [Column("client_name", TypeName = "varchar")]
        [DisplayName("Client Name")]
        [MaxLength(30, ErrorMessage = "Client Name should not be longer than 30 characters")]
        [MinLength(3, ErrorMessage = "Client Name is too short")]
        [Remote("doesClientNameExist", "Client", HttpMethod = "POST", ErrorMessage = "Client Name already exists. Please enter a different Client Name.")]
        public virtual string Name { get; set; }

        public int id { get; set; }
    }
}
So, now, after I open the Add dialog and type the first digit, the doesClientNoExist method fire. I want to be able to complete the input before firing the validations. I seem to have the similar problem with phone or email - the validation messages show right away after I start typing.

Is it the expected behavior or something is wrong?

Thanks a lot in advance.
If it's not broken, fix it until it is.


My Blog
Next
Reply
Map
View

Click here to load this message in the networking platform