Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6212299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:29:26+00:00 2026-05-24T06:29:26+00:00

My Model : public virtual int? NumberTest { get; set; } My View @Html.LabelFor(model

  • 0

My Model :

public virtual int? NumberTest { get; set; }

My View

@Html.LabelFor(model => model.NumberTest)
<br />
@Html.TextBoxFor(model => model.NumberTest)

I’m using Masked Input Plugin, so I have in my View :

$("#NumberTest").mask("99999-999");

My Html generated :

<input data-val="true" data-val-number="The field NumberTest must be a number." id="NumberTest" name="NumberTest" type="text" value="" />

So it automatically generated a number validation on my Integer input… And I’m using a mask with non-integer char to format number….

This validatior is always called when I fill the input … How can I fix that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:29:27+00:00Added an answer on May 24, 2026 at 6:29 am

    What I did was set the data type to string so it would work with maskedinput, but then in a custom model binder, I stripped out all the non-numeric characters so it could save to the database as an int. You still get both client-side and server-side protection because the user is prevented from entering non-numeric characters by maskedinput client-side and potentially bad characters are filtered out server-side.

    Here’s the custom model binder code:

    public class CustomModelBinder : DefaultModelBinder
    {
        protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value)
        {
            if (value != null && propertyDescriptor.PropertyType == typeof(string))
            {
                // always trim strings to clean up database padding
                value = ((string)value).Trim();
    
                if ((string)value == string.Empty)
                {
                    value = null;
                }
                else if ((propertyDescriptor.Attributes[typeof(PhoneNumberAttribute)] != null
                    || propertyDescriptor.Attributes[typeof(ZipCodeAttribute)] != null
                    || propertyDescriptor.Attributes[typeof(SocialSecurityNumberAttribute)] != null)
                    && bindingContext.ValueProvider.GetValue(propertyDescriptor.Name) != null
                    && bindingContext.ValueProvider.GetValue(propertyDescriptor.Name).AttemptedValue != null)
                {
                    value =
                        Regex.Replace(bindingContext.ValueProvider.GetValue(propertyDescriptor.Name).AttemptedValue,
                                      "[^0-9]", "");
                }
            }
    
            base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
        }
    }
    

    The custom attributes are just empty Attributes:

    public class ZipCodeAttribute : Attribute { }
    

    In the view model, just mark your field like this:

    [ZipCode]
    public string Zip { get; set; }
    

    Here’s how to do the whole thing with maskedinput, editor templates and unobtrusive validation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's my model public class Movie { public int MovieID { get; set; }
The Code-First model: public class Princess { public int Id { get; set; }
My Model: public class Country { public int CountryId { get; set; } public
Here is my model public class Horse { public int HorseId { get; set;
I have the following model entity: public class ScheduledTask { public virtual int ScheduledTaskId
I have entity class: public class Project { public virtual int Id { get;
I have the following model: public class Hotfix { public int? released_version { get;
I have a self-referential model class: public class Word { public virtual int Id
I'm using NHibernate 3.2 and have the following model: class User { public virtual
My model: public class Father { Set<Son> sons = new HashSet<Son>(); String name =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.