Validation of number of digits in a field

S

Simon Ransom

Hello

I am trying to validate user input into a UserForm field. The user has to
enter 8 digits however the number could be in the format of "00930123" with
leading zeros. I would prefer that they have to enter the zeros rather than
the input mask adding them.

Many thanks

Simon
 
H

Helmut Weber

Hi Simon,
there is not much you can do, but to check
whether the number of characters in field
equals 8 and whether each character is numeric.
If you need help on how to check that, let us know.
 
H

Helmut Weber

Hi Simon,
Maybe this is the wrong guess,
but if you are talking about a textbox on
a userform, the following could be one of
many possible solutions.
Private Sub TextBox1_Change()
Dim s As String
s = TextBox1.Text
If s = "" Then GoTo ende
If IsNumeric(Right(s, 1)) = False Then
s = Left(s, Len(s) - 1)
End If
ende:
If Len(s) > 7 Then s = Left(s, 8)
TextBox1.Text = s
End Sub
---
Just try it out.
Strictly speaking, if something entered,
that isn't a number, it will be removed at once.
With formfields on a form, which isn't a "userform",
I think, but just a "form", it wouldn't be that easy.
And I am no good at forms at all. Anyway, if any pointers
would be most welcome, try this as exitmacro of a
formfield.
Sub TestNum()
Dim s As String
Dim i As Integer
With ActiveDocument
s = .FormFields(1).Result
If Len(s) <> 8 Then
.FormFields(1).Result = "no way"
Exit Sub
End If
For i = 1 To 8
If Not IsNumeric(Mid(s, i, 1)) Then
MsgBox "noway"
.FormFields(1).Result = "no way"
Exit Sub
End If
Next
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top