Link to home
Create AccountLog in
Avatar of Victor  Charles
Victor CharlesFlag for United States of America

asked on

Help with validating data enetered in Textbox

Hello,

When I enter "+12" in my textbox I receive the following error:

Index and length must refer to a location within the string.  
Parameter name: length

How do i fix this error?

I am using a maskedInput control with the following format:&999

  If C1AccelerationDMin.Text <> "" And C1AccelerationDMin.TextLength = 4 Then
        Dim literal As String = C1AccelerationDMin.Text
        Dim substring As String = literal.Substring(0, 1)
        If substring <> "+" And substring <> "-" And C1AccelerationDMin.TextLength = 4 Then
        MsgBox("Data must be intered in the following format +/-XXX")
        End If
        Dim literalA As String = C1AccelerationDMin.Text
        Dim substringA As String = literalA.Substring(1, 3)
        If substringA > 333 Then
        MsgBox("GREATER THAN 333")
        End If
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

which line throws the error?
Avatar of Victor  Charles

ASKER

The error occurs on line:

Dim substringA As String = literalA.Substring(1, 3)
check for validity first, and use substring otherwise:
f C1AccelerationDMin.Text <> "" And C1AccelerationDMin.TextLength = 4 Then
        Dim literal As String = C1AccelerationDMin.Text
        Dim substring As String = literal.Substring(0, 1)
        If substring <> "+" And substring <> "-" And C1AccelerationDMin.TextLength = 4 Then
        MsgBox("Data must be intered in the following format +/-XXX")
        End If
        Dim literalA As String = C1AccelerationDMin.Text
if Not string.IsNullOrEmpty(literalA)
        Dim substringA As String = literalA.Substring(1)
        If substringA > 333 Then
        MsgBox("GREATER THAN 333")
        End If
end if

Open in new window

this also work...
 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If C1AccelerationDMin.Text <> "" And C1AccelerationDMin.TextLength = 4 Then
            Dim literal As String = C1AccelerationDMin.Text
            Dim substring As String = literal.Substring(0, 1)
            If substring <> "+" And substring <> "-" And C1AccelerationDMin.TextLength = 4 Then
                MsgBox("Data must be intered in the following format +/-XXX")
                C1AccelerationDMin.Focus()
                 C1AccelerationDMin.SelectAll
                
            End If
            Dim literalA As String = C1AccelerationDMin.Text

            Dim substringA As String = literalA.Substring(1, 3)
            If CType(substringA, Integer) > 333 Then
                MsgBox("GREATER THAN 333")
                C1AccelerationDMin.Focus()
                C1AccelerationDMin.SelectAll()
            End If
        End If
    End Sub

Open in new window

I need to make sure they enter all 4 characters and if the first character is not +/- set error message or if value is greater than 333 set error message. I will try your code and get back to you shortly.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Meir Rivkin
Meir Rivkin
Flag of Israel image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
add this error message sub:

public sub DisplayErroMessage
MsgBox("Data must be intered in the following format +/-XXX, and must be greater than 333")
C1AccelerationDMin.Focus()
                C1AccelerationDMin.SelectAll()
end sub

Open in new window


here's the validation sub:
Dim literal As String = C1AccelerationDMin.Text.Trim()
	if string.IsNullOrEmpty(literal) or literal.TextLength <> 4 then
	DisplayErroMessage
	return
end if

if literal(0) <> "-"c and literal(0) <> "+"c then
	DisplayErroMessage
	return
end if
   

If CType(literal.Substring(1), Integer) > 333 Then
	MsgBox("GREATER THAN 333")
else
	DisplayErroMessage
End If

Open in new window

did you try my code :(
It's working!

I am using the code below for a similar control

 If C1FTH.Text <> "" And C1FTH.TextLength <> 4 Then
        MsgBox("Incomplete Entry")
        End If
but no error is triggered when I enter only two characters.

I will replace with the code below:

Dim literal As String = C1FTH.Text.Trim()
      if string.IsNullOrEmpty(literal) or literal.TextLength <> 4 then
      DisplayErroMessage
      return
end if


and I believe this will solve the issue.

Thank You very much.

Victor
Hi again,

i am not sure why it's not working, the masked is 99.99, but when I enter "88" instead "88.55"
the error message does not trigger.

Code:

Dim literal As String = C1FTH.Text.Trim()
      if string.IsNullOrEmpty(literal) or literal.TextLength <> 5 then
      DisplayErroMessage
      return
end if
Hi Jtoutou,

Thanks for your code, I tried it but I was still getting the error message from my initial post.

Victor
For your initial post and mask &999 the code works...
for your last post i think there is mistake in
"if.......literal.TextLength <> 5 then
I will try it again.

Thanks.

Victor
With mask 99.99 what kind of validation do you want..?
I want to make sure all the values are entered, for example if they enter less than 4 characters an error should be triggered,
try this (mask 99,99)
  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click

        Dim Fstr() As String = C1AccelerationDMin.Text.Split(","c)
        If C1AccelerationDMin.Text = "" Or (Fstr(0).Count + Fstr(1).Count < 4) Then
            MessageBox.Show("error")
        End If

    End Sub

Open in new window

Just to confirm, I tried your code again and still got the same error on line:

Dim substringA As String = literalA.Substring(1, 3)


I will credit sedgwick for this post and open a new post for the second issue.

code:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        If C1AccelerationDMin.Text <> "" And C1AccelerationDMin.TextLength = 4 Then
            Dim literal As String = C1AccelerationDMin.Text
            Dim substring As String = literal.Substring(0, 1)
            If substring <> "+" And substring <> "-" And C1AccelerationDMin.TextLength = 4 Then
                MsgBox("Data must be intered in the following format +/-XXX")
                C1AccelerationDMin.Focus()
                 C1AccelerationDMin.SelectAll
               
            End If
            Dim literalA As String = C1AccelerationDMin.Text

            Dim substringA As String = literalA.Substring(1, 3)
            If CType(substringA, Integer) > 333 Then
                MsgBox("GREATER THAN 333")
                C1AccelerationDMin.Focus()
                C1AccelerationDMin.SelectAll()
            End If
        End If
    End Sub
Thank You!
victor sorry but i am confused...do you want validation with mask &999 or 99,99
Jtoutou,

I needed validation for both.

just tried your code below for 99.99 valdation

Dim Fstr() As String = C1AccelerationDMin.Text.Split("."c)
        If C1AccelerationDMin.Text = "" Or (Fstr(0).Count + Fstr(1).Count < 4) Then
            MessageBox.Show("error")
        End If

It works but if I enter a number at the end even if all 4 characters are not enetered the error message is not triggered.

For example

99.__   Error message triggered
__._9   Error message not triggered.

I will start a new post with the message above, to credit you for the solution for the second issue.

Thanks,

Victor
you are right just replace with this
 Dim Fstr() As String = C1AccelerationDMin.Text.Trim.Split(","c)