Validate date in code behind with regex – VB.NET

Code behind

'define public var
Protected StartDate As String
Protected EndDate As String

'define other var
Dim sDate = Request.Form("startDateTbx")
Dim eDate = Request.Form("endDateTbx")
Dim r As Regex 
r = New Regex("^(0[1-9]|[1-9]|1[012])/(0[1-9]|[1-9]|[12][0-9]|3[01])/20\d\d$") 

'run validation
If sDate = "" Then
     startDateLbl.Text = "Required"
 ElseIf Not r.Match(sDate).Success Then
    startDateLbl.Text = "Invalid date. Use mm/dd/yyyy."
 Else
    startDateLbl.Text = ""
End If

If eDate <> "" And Not r.Match(eDate).Success Then
   endDateLbl.Text = "Invalid date. Use mm/dd/yyyy."
Else
   endDateLbl.Text = ""
End If

StartDate = sDate
 EndDate = eDate


Markup page

<%=StartDate%>
<%=EndDate%>

Captcha – VB.NET

Email form

<tr>
  <td></td>
  <td></td>
</tr>
<tr>
  <td></td>
  <td><img style="WIDTH: 119px; HEIGHT: 34px" alt="" src="Captcha.aspx" /> 
<a href="">Can't Read?</a></td>
</tr>
<tr>
   <td></td>
   <td><asp:textbox runat="server" ID="txtcaptcha"></asp:textbox></td>
</tr>  
<tr>
    <td></td>
     <asp:ImageButton ID="ContactSeller" CausesValidation="True" runat="server" 
     ImageURL="/images/buttons/btn_submit.gif" /></td>
</tr>    

Email page code behind

....
 Dim captchaStr As String = Session("randomStr")
 Dim captchaTxt As TextBox = ContactSeller.FindControl("txtcaptcha")

If captchaStr <> captchaTxt.Text.ToString Then
   MsgCheck.Text = "<span style='color: red; font-weight: bold;'>
   Incorrect captcha.</span>"
Else
...
'your email code
End If

Captcha markup page

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Captcha.aspx.vb" 
Inherits="Captcha"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
    Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Captcha </title>
</head>

<body>
    <form id="form1" runat="server">
    <div>  
</div>
    
</body>
</html>


Captcha code behind

Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Text
Partial Class Captcha
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
                                                    Handles Me.Load
        'create object of Bitmap Class and set its width and height.
        Dim objBMP As Bitmap = New Bitmap(180, 51)
        'Create Graphics object and assign bitmap object to graphics' object.
        Dim objGraphics As Graphics = Graphics.FromImage(objBMP)
        objGraphics.Clear(Color.OrangeRed)
        objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias
        Dim objFont As Font = New Font("arial", 30, FontStyle.Regular)
        'genetating random 6 digit random number
        Dim randomStr As String = GeneratePassword()
        'set this random number in session
        Session.Add("randomStr", randomStr)
        objGraphics.DrawString(randomStr, objFont, Brushes.White, 2, 2)
        Response.ContentType = "image/GIF"
        objBMP.Save(Response.OutputStream, ImageFormat.Gif)
        objFont.Dispose()
        objGraphics.Dispose()
        objBMP.Dispose()
    End Sub
    Public Function GeneratePassword() As String
    'Below code describes how to create random numbers.some of the digits and letters
    'are ommited because they look same like "i","o","1","0","I","O".
        Dim allowedChars As String = "a,b,c,d,e,f,g,h,j,k,m,n,p,q,r,s,t,u,v,w,x,y,z,"
        allowedChars += "A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z,"
        allowedChars += "2,3,4,5,6,7,8,9"
        Dim sep() As Char = {","c}
        Dim arr() As String = allowedChars.Split(sep)
        Dim passwordString As String = ""
        Dim temp As String
        Dim rand As Random = New Random()
        Dim i As Integer
        For i = 0 To 6 - 1 Step i + 1
            temp = arr(rand.Next(0, arr.Length))
            passwordString += temp
        Next
        Return passwordString
    End Function
End Class

Content Limited by Word Count – VB.NET


Imports System.Text.RegularExpressions

Partial Class for_sale_Default
    Inherits System.Web.UI.Page

Protected Sub GridView1_RowDataBound(ByVal sender As Object, 
                                        ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
   Dim WordCountInt As Integer = CountWords(DataBinder.Eval(e.Row.DataItem, 
                                                               "description").ToString)
   Dim WordCountStr As String = DataBinder.Eval(e.Row.DataItem, "description").ToString

  If WordCountInt >= 40 Then
    Dim iNextSpace As Integer = WordCountStr.LastIndexOf(" ", 175)
    e.Row.Cells(3).Text = + 
    DataBinder.Eval(e.Row.DataItem, "description").ToString.Substring(0, iNextSpace) + 
    " ... (<a href='Description.aspx?cat=1&aid=" + 
    DataBinder.Eval(e.Row.DataItem, "id").ToString + "' class='colorbox2'>more</a>)"
     e.Row.Cells(3).ToolTip = DataBinder.Eval(e.Row.DataItem, "description").ToString
  Else
     e.Row.Cells(3).Text = + DataBinder.Eval(e.Row.DataItem, "description").ToString
  End If
 End If
End Sub
    Public Function CountWords(ByVal value As String) As Integer
        ' Count matches.
        Dim collection As MatchCollection = Regex.Matches(value, "\S+")
        Return collection.Count
    End Function


End Class