Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Format UK Mobile number

Status
Not open for further replies.

saintedmunds

Technical User
Apr 7, 2006
78
GB
Hi

Im trying to check the format of a mobil phone number and if its incorrect change it to the correct format.

so lets say I have 07723 567432

I need to change it to 447723567432

I have no idea on regular expressions or if this is the way to go.

Could some one point me in the right direction

Thank you
 
This is what I have come up with
Could it be made better?

Private Sub cCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cCheck.Click

If CheckNumberFormat(cMobileNumber.Text) Then
MsgBox("This is a mobile Number")
ElseIf MobileNumberReFormat(cMobileNumber.Text) > "" Then
MsgBox("This is a mobile Number " & MobileNumberReFormat(cMobileNumber.Text))
Else
MsgBox("NOT a Mobile Number")
End If
End Sub


Function CheckNumberFormat(ByVal MobileNumber As String) As Boolean
If Len(MobileNumber) = 12 Then
If Microsoft.VisualBasic.Left(MobileNumber, 3) = "447" Then
Return True
End If
End If
End Function

Function MobileNumberReFormat(ByVal MobileNumber As String) As String
Dim MyNumber As String
MyNumber = Replace(MobileNumber, " ", "")
MyNumber = Replace(MyNumber, "+", "")

If CheckNumberFormat(MyNumber) Then
Return MyNumber
Exit Function
End If

If Microsoft.VisualBasic.Left(MyNumber, 1) = "0" Then
MyNumber = Microsoft.VisualBasic.Right(MyNumber, Len(MyNumber) - 1)
MyNumber = "44" & MyNumber
Else
MyNumber = "44" & MyNumber
End If

If CheckNumberFormat(MyNumber) Then
Return MyNumber
Exit Function
End If

Return ""

End Function
 
A regex to check the string would look something like :

^447\d{9}$

In terms of writing a reg ex to handle the transformation, I wouldn't know - not used them for that yet...

mmilan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top