Validate a string char-by-char

Jaymond Flurrie

Well-known Member
Joined
Sep 22, 2008
Messages
919
Office Version
  1. 365
Platform
  1. Windows
So, I'm trying to validate a string that can have only dots and numbers, but there can't be two dots in a row. String has to begin with a number and end to a dot

So these are ok
1.1.1.1.1.1.
and
2.452443.1.5.21.5.42131.

but
1..1.
and
1.1.t.
and
.1.1.
and
1.1
are not ok.

How to do this?
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
So, I'm trying to validate a string that can have only dots and numbers, but there can't be two dots in a row. String has to begin with a number and end to a dot

So these are ok
1.1.1.1.1.1.
and
2.452443.1.5.21.5.42131.

but
1..1.
and
1.1.t.
and
.1.1.
and
1.1
are not ok.

How to do this?

Why is 1..1. unacceptable ?
 
Upvote 0
Try:

PHP:
=AND(ISNUMBER(LEFT(A1,1)+0),SUBSTITUTE(A1,"..","")=A1,RIGHT(A1,1)=".",ISNUMBER(SUBSTITUTE(A1,".","")+0))

which will yield TRUE if it is ok, otherwise FALSE

You can use this in Conditional Frmatting or Validation, up to you. :)
 
Upvote 0
Try:

PHP:
=AND(ISNUMBER(LEFT(A1,1)+0),SUBSTITUTE(A1,"..","")=A1,RIGHT(A1,1)=".",ISNUMBER(SUBSTITUTE(A1,".","")+0))
which will yield TRUE if it is ok, otherwise FALSE

You can use this in Conditional Frmatting or Validation, up to you. :)

Looks good. Is there a VBA version for this? I mean, I probably can pretty easily translate this to VBA, but how about that "isnumber"-function?
 
Upvote 0
I would probably use a Regex function:
Code:
Function IsValid(str As String)
With CreateObject("vbscript.regexp")
    .Global = True
    .Pattern = "\d\."
        If .test(str) Then
            IsValid = .Replace((str), "") = ""
        End If
End With
End Function
so:
Code:
Msgbox IsValid(Range("A1"))
...would yield TRUE if it meets your conditions, otherwise FALSE
 
Upvote 0
I would probably use a Regex function:
Code:
Function IsValid(str As String)
With CreateObject("vbscript.regexp")
    .Global = True
    .Pattern = "\d\."
        If .test(str) Then
            IsValid = .Replace((str), "") = ""
        End If
End With
End Function
so:
Code:
Msgbox IsValid(Range("A1"))
...would yield TRUE if it meets your conditions, otherwise FALSE

Thank you. Can you shortly explain how this works, especially what is this .Pattern = "\d\." ?
 
Upvote 0
I just spotted a problem with the function. It will not match
24.4.

it expects a single numeric character, and then a dot.

Use this instead:
Rich (BB code):
Function IsValid(str As String)
With CreateObject("vbscript.regexp")
    .Global = True
    .Pattern = "\d+\."
        If .test(str) Then
            IsValid = .Replace((str), "") = ""
        End If
End With
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,389
Messages
6,119,232
Members
448,879
Latest member
VanGirl

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top