开发者

How to validate two textboxes at single button click using vb.net?

How to validate two textboxes at single button click using vb.net ?

it shows an error :

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
    If TextBox1 = "06:00:01 PM" & TextBox2.Text = "02:00:01 PM" Then
        MsgBox "Submit"
    End If
End Sub

Whatz wrong with my this code ? I wanna validate two checkbox using if state开发者_运维问答ment ... on Button click


First of all, it always helps if you share what the error message is. But there are just so many things wrong here:

  1. & doesn't mean what you seem think it does. It's the string concatenation operator and has nothing to do with a logical AND. You want "And" or "AndAlso".
  2. Don't forget to use your TextBox's .Text property.
  3. Never NEVER NEVER call the MsgBox function in ASP.Net. When deployed to an unattended web server (the normal case), it will display on a private desktop created just for that purpose and block until clicked on, which, given that it's showing on a private destkop, can never happen.
  4. Your users will hate you for forcing them to manually enter such a specific datetime format.
  5. Use the ASP.Net validation controls for this. It's dirt simple.


You cannot get the value of a TextBox from just using TextBox1 To get the value, you need to use TextBox1.Text

Also, you can't use & the way you did, since that's used to concatenate strings. You need AndAlso.

Try this

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
    If TextBox1.Text = "06:00:01 PM" AndAlso TextBox2.Text = "02:00:01 PM" Then
        ''# This is Valid
    Else
        ''# Sorry, Try Again
    End If
End Sub

Though I would suggest using the built in ASP.NET validation... I'm not sure if you have to validate those times exactly, but you could try a RegularExpressionValidator and a RequiredFieldValidator instead.

This could be your Regex for the DateTime

"^((((([13578])|(1[0-2]))[\-\/\s]?(([1-9])|([1-2][0-9])|(3[01])))|((([469])|(11))[\-\/\s]?(([1-9])|([1-2][0-9])|(30)))|(2[\-\/\s]?(([1-9])|([1-2][0-9]))))[\-\/\s]?\d{4})(\s((([1-9])|(1[02]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$"


Try changing:

If TextBox1 = "06:00:01 PM" & TextBox2.Text = "02:00:01 PM" Then

To:

If TextBox1.Text = "06:00:01 PM" AndAlso TextBox2.Text = "02:00:01 PM" Then

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜