Victor Charles
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.TextLen gth = 4 Then
Dim literal As String = C1AccelerationDMin.Text
Dim substring As String = literal.Substring(0, 1)
If substring <> "+" And substring <> "-" And C1AccelerationDMin.TextLen gth = 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
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.TextLen
Dim literal As String = C1AccelerationDMin.Text
Dim substring As String = literal.Substring(0, 1)
If substring <> "+" And substring <> "-" And C1AccelerationDMin.TextLen
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
which line throws the error?
ASKER
The error occurs on line:
Dim substringA As String = literalA.Substring(1, 3)
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
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
ASKER
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.
Thanks.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
add this error message sub:
here's the validation 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
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
did you try my code :(
ASKER
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(liter al) or literal.TextLength <> 4 then
DisplayErroMessage
return
end if
and I believe this will solve the issue.
Thank You very much.
Victor
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(liter
DisplayErroMessage
return
end if
and I believe this will solve the issue.
Thank You very much.
Victor
ASKER
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(liter al) or literal.TextLength <> 5 then
DisplayErroMessage
return
end if
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(liter
DisplayErroMessage
return
end if
ASKER
Hi Jtoutou,
Thanks for your code, I tried it but I was still getting the error message from my initial post.
Victor
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
for your last post i think there is mistake in
"if.......literal.TextLength <> 5 then
ASKER
I will try it again.
Thanks.
Victor
Thanks.
Victor
With mask 99.99 what kind of validation do you want..?
ASKER
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
ASKER
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.TextLen gth = 4 Then
Dim literal As String = C1AccelerationDMin.Text
Dim substring As String = literal.Substring(0, 1)
If substring <> "+" And substring <> "-" And C1AccelerationDMin.TextLen gth = 4 Then
MsgBox("Data must be intered in the following format +/-XXX")
C1AccelerationDMin.Focus()
C1AccelerationDMin.SelectA ll
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.SelectA ll()
End If
End If
End Sub
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.TextLen
Dim literal As String = C1AccelerationDMin.Text
Dim substring As String = literal.Substring(0, 1)
If substring <> "+" And substring <> "-" And C1AccelerationDMin.TextLen
MsgBox("Data must be intered in the following format +/-XXX")
C1AccelerationDMin.Focus()
C1AccelerationDMin.SelectA
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.SelectA
End If
End If
End Sub
ASKER
Thank You!
victor sorry but i am confused...do you want validation with mask &999 or 99,99
ASKER
Jtoutou,
I needed validation for both.
just tried your code below for 99.99 valdation
Dim Fstr() As String = C1AccelerationDMin.Text.Sp lit("."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
I needed validation for both.
just tried your code below for 99.99 valdation
Dim Fstr() As String = C1AccelerationDMin.Text.Sp
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.Tr im.Split(" ,"c)
Dim Fstr() As String = C1AccelerationDMin.Text.Tr