SSN Validation in MVC2 - asp.net-mvc

I have a business requirement where in i need to display SSN Field into three separate text boxes and if any one of the textbox is blank and user clicks save, I need to display a common error message on top of the SSN Field saying- "SSN is required field". I have added required field attribute on the view model on all these fields so i am getting three edit messages saying SSN is required. how to present only one edit in this case ?
I am using ASP.NET MVC 2
My view model is like this
[Required]
SSN_FirstThreeDigits
[Required]
SSN_SecondTwoDigits
[Required]
SSN_ThirdFourDigits
Any suggestions
Thanks
Subu

SSNValidator.com now has an API for validating social security numbers. Data elements include whether or not the number was issued, approximate date of issuance, state of issuance and if it was used in a death claim. Go to the site and click the API/Batch Processing links for details.

The Mvc.ValidationTookit Alpha Release: Conditional Validation with MVC 3 can do this.

You may need a custom model binder. Please have a look at this article and see how he validates date of birth

Related

ASP.NET MVC3 Thousand separator in a decimal field is not recognized when user submits form

I'm creating an ASP.NET MVC3 application, and I have a form where user have to enter a price. For improving user experience, I'd like to implement a thousand separator. I've done it, you can see it working on this FIDDLE.
BUT
This Price field is part of a model and is declared in my Model file as
public decimal? Price {get; set;}
When I submit my form, Price is always null. However, it has the value entered in the form if I remove my thousand separator. This is because Price seems to be interpreted as a string, and not as a decimal, so MVC doesn't match the two properties (I suppose).
So, how can force MVC to recognize my value as a correct decimal and submit a model with the price entered by the user ?
Thanks for your help.
I believe it will be easier to make the following: you can either create a hidden field on the form and store the number without separators or you can remove separators right before submitting the form.
Example:
$("#target-form").submit(function(event) {
value_with_separators = $('.thousandSeparator').val();
clean_value = value_with_separators.replace(/\s+/g, '');
$('.thousandSeparator').val(clean_value);
});
There's also another way. You can implement custom ModelBinder and perform required conversions there. Here is a link to a related question with code samples.
Hope it helps!

MVC Show different ValidationSummary ErrorMessage compared to ValidationMessage

In ASP.NET MVC, is it possible to show a different message in the ValidationSummary compared to what is shown in the ValidationMessage?
IE - if i have a FirstName textbox, on validation the message next to the text box will say 'You need to fill this out', but in the validation summary it will say 'Please provide a first name'.
I'm not sure if I completely understand what you are trying to accomplish, but you can specify a generic error message in the validation summary. In your view you can use:
#Html.ValidationSummary(true, "Please correct the errors below")
The boolean parameter indicates whether you want to exclude property errors. The string is the message you want displayed. Using this overload the way I have above, the model-level error message Please correct the errors below would be shown in place of the #Html.ValidationSummary() method, and the property errors would be shown where you place your #Html.ValidationMessageFor() methods.
See the MSDN documentation for a complete list of overloads.
Yes. It should be quite obvious from looking at the intellisense.
you would say:
#Html.ValidationSummary(true)
and it will contain the error messages that are located on the model or the default messages
And you can say:
#Html.ValidationMessageFor(m => m.Property, "This is a custom message")
And that overrides the message on the individual message.
Also, keep in mind that #Html.ValidationSummary(true, 'Header Message') will show your custom summary message along with the same messages that you provided in your model data annotations.
i.e. in your model class:
[Required(Message="First name is required")]
public string FirstName { get; set; }
Your validation summary would look some like this:
Header Message
First name is required
Alternatively, you can build up a custom collection of messages by using ModelState.AddModelError("Key", "Message") in your controller, then referencing that key in your view using ViewData.
After looking at the Metadata available, I don't think what I want to do is possible. What I'm going to do instead is type in the input specific value for validation, and hide the class field-validation-valid.
Providing different Errormesages beside the input fields and in the Validation Summary is not possible with #Html.ValidationSummary. Such a feature could make sense because a part of the errormessage's information can come from it's position in the page (e.g. beside a firstname input field the message could be 'Input required' and in the validation summary you need 'Input required for firstname'). Unfortunately a poor implementation for ValidationSummary in MVC, .net 2.0 provided the feature to have different messages in it's validators and in the validationsummary .

ASP.NET MVC jQuery validation: how to show one message for all required fields

I got many [Required] fields in my model.
And, in traditional MVC way, I will add #Html.ValidationMessageFor() for each required textbox.
Then, if user doesn't type anything for 5 textboxes, 5 message will show up in the UI.
My question is: can I show just one message saying Please fill mandatory fields in the UI instead of showing the message everywhere like Name is required, Address is required...?
and I need this to be validate on Client Side, without post form back to server
You might want to look at ValidationSummary - http://msdn.microsoft.com/en-us/library/system.web.mvc.html.validationextensions.validationsummary(v=vs.108).aspx - Returns an unordered list (ul element) of validation messages that are in the ModelStateDictionary object. You can override the message like so:
#Html.ValidationSummary(true, "Invalid form")
Source https://stackoverflow.com/a/4924494/201648
You can use #Html.ValidationSummary:
#Html.ValidationSummary(true, "Please fill mandatory fields")
If you also want to include a list of property specific errors use:
#Html.ValidationSummary(false, "Please fill mandatory fields")

Easiest way to alter required validation message site wide

I would like to replace validation message for all model properties that have [Required] attribute from the default "The XY field is required." to "*". I want to do that site wide in one place without having to add a custom message to Required attributes or passing additional parameter to ValidationMessageFor() Html helper.
Any ideas?
See the answer to my question here :
Localizing Error Messages In ASP.NET MVC 2 (Default Validation Attributes)

asp.net mvc how to save?

I have the following model:
Customer:
ID
Name
Address
Phone
Fax
I added an Edit view based on the above model from the controller. I modified the Edit view to only allow edit on the Phone and Fax field (deleted the rest). When I submit it I get an error. It works if I leave the Edit view untouched (5 fields). However I only want to allow change in the last 2 fields.
I am lost, please help. Thanks :)
If you are using the MVC ability to populate your entity/class i.e. your action sig looks like this:
ViewResult MyAction(MyObject object) {
...
Save(MyObject);
}
then you'll need to make sure you include the other field, non-editable, either as visible information or using Html.Hidden within the form scope to ensure you have a fully populated object. Remember, the web is stateless and the server has no idea which record you were editing unless it has the keys to do so retrospectively.
The other option would be to retrieve the original object (for which you'll still need the primary key) from the database, update the fields from your form data and then submit the changes. We'd need to know the specific error to be able to help further, the code you are using would also be a great help.
Without knowing more I would guess it has something to do with binding null to a non null property in your model. Can you give me more details on the model, the error.
If you are using the default mvc model binder then it will only bind the fields you submit. So either submit as hidden or dont use a model binder and manually map the variable from Request.Form into a copy of the model you pulled form the db.

Resources