Search code examples
c#asp.netasp.net-mvcvalidationasp.net-mvc-4

VIN Number Validation Using ASP.NET MVC


I am using MVC 4 for a current project. Project generally deals with Vehicles and identifying the correct vehicles for different usages.

I have the below model.

public class Vehicle
{
   [Required]
   public string VIN{get; set;}
   //
}

I want to verify the correct incoming VIN number. I am not sure what is the correct/easiest way to validate something like a VIN number. I have seen some validation examples, none seemed to work for me. Need some help on Client side validation. Thanks.


Solution

  • I'm using RegularExpression for validating VIN in my own project. Try the below example:

    public class Vehicle
    {
        [RegularExpression("[A-HJ-NPR-Z0-9]{13}[0-9]{4}", ErrorMessage = "Invalid Vehicle Identification Number Format.")]
        public string VIN { get; set; }
    }
    

    VIN usually consist of 17 characters.

    The very first letter or number of the VIN tells you in what region of the world your vehicle was made.

    The second letter or number, in combination with the first letter or number in the VIN, tells you in what country the car or truck was made.

    The third number or letter is used by the vehicle manfacturer to identify what kind of vehicle it is.

    The 4th 5th 6th 7th 8th characters, you can find out the vehicle model, engine type, body style.

    The 9th character is the VIN check digit where you can use math to figure out if it is a correct VIN.

    The 10th letter or number of the VIN tells you the model year of the vehicle.

    The 11th 12th 13th 14th 15th 16th characters is where the auto manufacturers enter unique information about the particular vehicle the VIN belongs to.

    Hope it helps!