我在当前的项目中使用MVC 4。项目一般涉及车辆,并为不同用途确定正确的车辆。
我有下面的型号。
public class Vehicle
{
[Required]
public string VIN{get; set;}
//
}我要确认正确的输入VIN号码。我不知道什么是正确的/最简单的方法来验证像VIN号码这样的东西。我已经看到了一些验证的例子,似乎没有一个对我有用。在客户端验证方面需要一些帮助。谢谢。
发布于 2013-11-27 03:20:05
我使用RegularExpression在我自己的项目中验证VIN。试试下面的示例:
public class Vehicle
{
[RegularExpression("[A-HJ-NPR-Z0-9]{13}[0-9]{4}", ErrorMessage = "Invalid Vehicle Identification Number Format.")]
public string VIN { get; set; }
}文通常由17个字符组成。
VIN的第一个字母或号码告诉您您的车辆是在世界的哪个地区制造的。
第二个字母或数字,结合VIN中的第一个字母或数字,告诉您汽车或卡车是在哪个国家制造的。
第三个数字或字母是由车辆管理人员使用,以确定它是什么样的车辆。
第四、第五、第六、第七、第八字,可以找出汽车型号、发动机型号、车身风格。
第九个字符是VIN校验数字,您可以使用数学来判断它是否是正确的VIN。
VIN的第十个字母或号码告诉您车辆的型号年份。
第11,12,13,14,15,16字符是汽车制造商输入关于VIN所属的特定车辆的独特信息的地方。
希望能帮上忙!
发布于 2018-02-01 18:54:50
以下是C#中的@Here敲门答案
public static bool ValidateVin(string vin)
{
if (vin.Length != 17)
return false;
var result = 0;
var index = 0;
var checkDigit = 0;
var checkSum = 0;
var weight = 0;
foreach (var c in vin.ToCharArray())
{
index++;
var character = c.ToString().ToLower();
if (char.IsNumber(c))
result = int.Parse(character);
else
{
switch (character)
{
case "a":
case "j":
result = 1;
break;
case "b":
case "k":
case "s":
result = 2;
break;
case "c":
case "l":
case "t":
result = 3;
break;
case "d":
case "m":
case "u":
result = 4;
break;
case "e":
case "n":
case "v":
result = 5;
break;
case "f":
case "w":
result = 6;
break;
case "g":
case "p":
case "x":
result = 7;
break;
case "h":
case "y":
result = 8;
break;
case "r":
case "z":
result = 9;
break;
}
}
if (index >= 1 && index <= 7 || index == 9)
weight = 9 - index;
else if (index == 8)
weight = 10;
else if (index >= 10 && index <= 17)
weight = 19 - index;
if (index == 9)
checkDigit = character == "x" ? 10 : result;
checkSum += (result * weight);
}
return checkSum % 11 == checkDigit;
}发布于 2016-04-05 20:12:38
VINs是使用第9位的校验数字和公式验证的,您可以很容易地找到。下面是我编写的验证的VB版本。我是一个新的VB程序员,所以我确信有更优雅的方法,但逻辑是正确的."all one's“(请参阅注释行)是对任何VIN验证算法的良好检查。
Function ValidateVin(vin As String) As Boolean
'Dim vin As String = "2G1FC3D33C9165616"
'Dim vin As String = "11111111111111111"
Dim vinCharArray() = vin.ToCharArray()
Dim result As Int32
Dim index As Int32 = 0
Dim checkDigit As Int32
Dim checkSum As Int32 = 0
Dim weight As Int32
If vin.Length <> 17 Then
Console.WriteLine(vbCrLf + "Supplied VIN does not have 17 characters, please check and try again" + vbCrLf)
Return False
Else
For Each i As Char In vinCharArray
index += 1
If Asc(i) > 47 And Asc(i) < 58 Then
result = Int32.Parse(i)
Else
Select Case Char.ToLower(i)
Case "a", "j"
result = 1
Case "b", "k", "s"
result = 2
Case "c", "l", "t"
result = 3
Case "d", "m", "u"
result = 4
Case "e", "n", "v"
result = 5
Case "f", "w"
result = 6
Case "g", "p", "x"
result = 7
Case "h", "y"
result = 8
Case "r", "z"
result = 9
End Select
End If
Select Case index
Case 1 To 7, 9
weight = 9 - index
Case 8
weight = 10
Case 10 To 17
weight = 19 - index
End Select
If index = 9 Then
If Char.ToLower(i) = "x" Then
checkDigit = 10
Else
checkDigit = result
End If
End If
'Console.WriteLine("Index {0} has a value of {1} and a weight of {2}", index, result, weight)
checkSum += (result * weight)
Next
'Console.WriteLine("checksum is {0}", checkSum)
'Console.WriteLine("checkdigit is {0}", checkDigit)
Dim checksOut As Boolean
If checkSum Mod 11 = checkDigit Then
checksOut = True
End If
'Console.WriteLine(checksOut)
'Console.ReadLine()
Return checksOut
End If
End Functionhttps://stackoverflow.com/questions/20233167
复制相似问题