2010-03-25 85 views
5

有誰知道如何檢查有效的IMEI嗎?檢查有效的IMEI

我已經找到了一個函數來檢查這個頁面上:http://www.dotnetfunda.com/articles/article597-imeivalidator-in-vbnet-.aspx

但它對於有效的IMEI的指標(如352972024585360)返回false。 我可以驗證他們在網頁上:http://www.numberingplans.com/?page=analysis&sub=imeinr

什麼是正確的方式(在VB.Net中)來檢查給定的IMEI是否有效?

PS: 這從網頁上面的功能必須以某種方式不正確:

Public Shared Function isImeiValid(ByVal IMEI As String) As Boolean 
    Dim cnt As Integer = 0 
    Dim nw As String = String.Empty 
    Try 
     For Each c As Char In IMEI 
      cnt += 1 
      If cnt Mod 2 <> 0 Then 
       nw += c 
      Else 
       Dim d As Integer = Integer.Parse(c) * 2 ' Every Second Digit has to be Doubled ' 
       nw += d.ToString() ' Genegrated a new number with doubled digits ' 
      End If 
     Next 
     Dim tot As Integer = 0 
     For Each ch As Char In nw.Remove(nw.Length - 1, 1) 
      tot += Integer.Parse(ch) ' Adding all digits together ' 
     Next 
     Dim chDigit As Integer = 10 - (tot Mod 10) ' Finding the Check Digit my Finding the Remainder of the sum and subtracting it from 10 ' 
     If chDigit = Integer.Parse(IMEI(IMEI.Length - 1)) Then ' Checking the Check Digit with the last digit of the Given IMEI code ' 
      Return True 
     Else 
      Return False 
     End If 
    Catch ex As Exception 
     Return False 
    End Try 
End Function 

編輯:這是我的工作 「checkIMEI」 - 功能:

Public Shared Function checkIMEI(ByRef IMEI As String) As Boolean 
    Const allowed As String = "" 

    Dim cleanNumber As New System.Text.StringBuilder 
    For i As Int32 = 0 To IMEI.Length - 1 
     If (allowed.IndexOf(IMEI.Substring(i, 1)) >= 0) Then 
      cleanNumber.Append(IMEI.Substring(i, 1)) 
     End If 
    Next 

    If cleanNumber.Length <> 15 Then 
     Return False 
    Else 
     IMEI = cleanNumber.ToString 
    End If 

    For i As Int32 = cleanNumber.Length + 1 To 16 
     cleanNumber.Insert(0, "0") 
    Next 

    Dim multiplier As Int32, digit As Int32, sum As Int32, total As Int32 = 0 
    Dim number As String = cleanNumber.ToString() 

    For i As Int32 = 1 To 16 
     multiplier = 1 + (i Mod 2) 
     digit = Int32.Parse(number.Substring(i - 1, 1)) 
     sum = digit * multiplier 
     If (sum > 9) Then 
      sum -= 9 
     End If 
     total += sum 
    Next 

    Return (total Mod 10 = 0) 
End Function 
+0

我有同樣的問題!我的VBA驗證器例程(從維基百科鏈接獲取vb)和您的dotnetfunda.com頁面拒絕了在新手機盒子上掃描的IMEI號碼。你有什麼消息嗎? – 2010-05-28 12:13:28

+0

我編輯了我的問題並添加了我的解決方案。 – 2010-05-28 12:24:40

回答

9

IMEI號碼使用Luhn算法進行驗證。鏈接頁面具有各種語言的實現。

+0

謝謝你的提示。 – 2010-03-25 19:45:08