logo  VB.Net - String Validation
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
Is Valid UK Postcode
It would be impossible to absolutely guarantee a postcode is valid without searching a list of over 1,500,000 current valid postcodes and there are frequent minor adjustments to the list. This function uses an official "regular expression". False indicates that "it is very likely" to be invalid. True indicates that is is very likely to be either vaild (but it could be expired.)

Public Function IsValidUkPostcode(pc As String) As Boolean
 Dim reg1 As New System.Text.RegularExpressions.Regex("^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([AZa-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))[0-9][A-Za-z]{2})$")
 Dim m As System.Text.RegularExpressions.Match = reg1.Match(pc)
 Return m.Success
End Function
  
IsValidUkPhoneNumber
The is no simple formula to guarantee that a UK phone number is valid. Some valid UK phone numbers are "premium rate" - they can be called (hence they are valid) but dialling the number could result in expensive call charges. This function reports false when the number is "obviously" impossible.

Public Function IsValidPhone(ph As String) As Boolean
 Dim reg1 As New System.Text.RegularExpressions.Regex("^(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?$")
 Dim m As System.Text.RegularExpressions.Match = reg1.Match(ph)
 Return m.Success
End Function
  
IsValidEmail
There is no guarantee that any email is valid. In today's world single use and temporary addresses are common. This routine can only dtect obvious problems in the email address structure.

Public Function IsValidEmail(em As String) As Boolean
 Dim reg1 As New System.Text.RegularExpressions.Regex("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$.")
 Dim m As System.Text.RegularExpressions.Match = reg1.Match(em.ToUpper)
 Return m.Success
End Function
  

DigitalDan.co.uk