logo  VB.Net - Validate
This Chapter
Validation 1
Validation 2
Chapters
Home Page
Colours, RGB
Computer Specifications
Dates&Times
Disk Drives
Files
Folders
GPS and OS Ref
VB.Net Forms
Image Files
If & Select
List/Array
Mathematics
NuGet
Sound
String Functions
Sun and Moon
User Controls
Validation
DigitalDan Sites
My Other Sites
Contact Site

Note
Some pages
may contain
inaccuracies
Hits=5
These functions validate strings. This means that they examine the contents of a string and decide whether it could be a real email-address, ohine number, date etc. Although some functions can offer a guaranteed result (e.g is a string a valid number) others can only indicate the string is plausible. (e.g. We can establish a string could be a postcode but cannot guarantee that the specific postcode has been issued and has not been withdrawn.)
Only contains A-Z, a-z, 0-9 and space

Public Function IsAlphaNumeric(s As String) As Boolean
 Dim r As New Regex("^[a-zA-Z0-9 ]*$")
 Return r.IsMatch(s)
End Function
  
Is a Valid Integer Number
Be aware this will accept anything VB.Net would consider an integer e.g -10, 17, +20, 1E3 (interpretted by VB as 1000). It will return false for non-integers e.g 99999999999999 (too big to fit Integer variable), 0.01 (is decimal not integer), 1E-2 (decimal not integer), 1Z2 (contains letter Z)

Public Function IsInteger(s As String) As Boolean
 Dim i As Integer
 Return Integer.TryParse(s, i)
End Function
  
Is a Valid Double Number
This will accept anything VB.Net could instert into a variable of type Double (e.g. 1.2, -3.4, 5, 1E3, 1E-7) It will return false for anything that could not fit in a Double e.g ZZZ (not a number) 1E500 (to big to fit in Double)

Private Function IsDouble(s As String) As Boolean
 Dim d As Double
 Return Double.TryParse(s, d)
End Function
  
Other Validation

Private Function IsPlausible_UK_NationalInsuranceNumber(s As String) As Boolean
 Dim r As New System.Text.RegularExpressions.Regex("^\b([A-CEGHJ-PR-TW-Z]{1}[A-CEGHJ-NPR-TW-Z]{1}(?<!BG|GB|NK|KN|TN|NT|ZZ))[0-9]{6}[A-DFM]{1}\b$")
 Return r.IsMatch(s)
End Function
'
Private Function IsPlausible_MAC_Address(s As String) As Boolean
 Dim r As New System.Text.RegularExpressions.Regex("^([0-9a-fA-F][0-9a-fA-F]:){5}([0-9a-fA-F][0-9a-fA-F])$")
 Return r.IsMatch(s)
End Function
'
Private Function IsDate(yyyy As Integer, mm As Integer, dd As Integer) As Boolean
 If yyyy < 1752 OrElse yyyy > 2199 OrElse mm < 1 OrElse mm > 12 OrElse dd < 1 Then Return False
 Return dd <= Date.DaysInMonth(yyyy, mm)
End Function
'
Private Function IsPlausible_UK_Passport_Number(s As String) As Boolean
 Dim r As New System.Text.RegularExpressions.Regex("\b\d{9}\b")
 Return r.IsMatch(s)
End Function
'
Private Function IsPlausible_UK_DrivingLicence_Number(s As String) As Boolean
 Dim r As New System.Text.RegularExpressions.Regex("\b[\w9]{5}\d{6}[\w9]{2}\d{5}\b")
 Return r.IsMatch(s)
End Function
'
Private Function IsPlausible_UK_NHS_Number(s As String) As Boolean
 Dim r As New Regex("\b\d{3}\s\d{3}\s\d{4}\b")
 Return r.IsMatch(s)
End Function
  
Is a Plausible Bank Card Number
It is impossible for any function to guarantee a card number is valid. This will only detect numbers that fail over simplistic tests. It is possible to reliably create false numbers that would evade this function and it could fail to detect some genuine typing errors. It is not suitable for commercial purposes.

Private Function IsPlausible_BankCardNumber(s As String) As Boolean
 s = s.Replace(" "c, "") ' users often add extra spaces
 Dim plausible As Boolean = True
 Dim len As Integer = s.Length
 If len < 13 OrElse len > 19 Then Return False
 Dim parity As Integer = len Mod 2
 Dim sum As Integer = 0
 Dim d As Integer
 For i As Integer = 1 To s.Length - 1
  If Not Integer.TryParse(Mid(s, i, 1), d) Then
   plausible = False
   Exit For
  End If
  If i Mod 2 = parity Then d *= 2
   If d > 9 Then d -= 9
   sum += d
  Next
  If Not plausible Then Return False
  sum = sum Mod 10
  If Not Integer.TryParse(Mid(s, 1, s.Length), d) Then
   Return False
  End If
 Return sum = d
End Function
  

DigitalDan.co.uk