Validation for data entry on the web page

Sometimes we need the validation for the data entry from the front web pages, the following is a few ways to implement

Solution 1:

<BDP:BDPLite ID="BDPLiteDateA" runat="server" />
<asp:RequiredFieldValidator ID="RequiredFieldValidatorDateA" runat="server" ErrorMessage=" * Required" ControlToValidate="BDPLiteDateA" Display="Dynamic">
</asp:RequiredFieldValidator> &nbsp;<br>

<asp:CustomValidator ID="CustomValidatorDateA" runat="server" ErrorMessage="* Date is not in the range of the XXXXXX" ControlToValidate ="BDPLiteDateA" Display ="Dynamic" OnServerValidate="CustomValidatorDateAbsent_ServerValidate">
Protected Sub CustomValidatorDateReceived_ServerValidate(ByVal sender As Object, ByVal args As ServerValidateEventArgs)
        Dim initialdate As New Date(1990, 1, 1)
        If (Date.Compare(CDate(args.Value), initialdate) > 0) Then
            args.IsValid = True
        ElseIf BDPLiteDateA.IsNull And BDPLiteDateA.IsDate Then
            args.IsValid = True
            args.IsValid = False
        End If
End Sub

Also it can use the following code to display more make sense error message to the client

Protected Sub CustomValidatorDateA_ServerValidate(ByVal sender As Object, ByVal args As ServerValidateEventArgs)
        If Controller.VerifyDateA(CDate(args.Value), XXXID) Then
            args.IsValid = True
            args.IsValid = False
        End If

        Dim C As CustomValidator = CType(sender, CustomValidator)
        C.ErrorMessage = Controller.getDateRangeString(Me.XXXID)
End Sub

Solution II: Javascript

Aspx page:

<asp:TextBox ID="txtXXXX" Width="80px" Text='’
onchange=”javascript_regcheck(this);” AutoPostBack=”false” TabIndex=”20″ runat=”server” />

Javascript function:

function regcheckTimeSeconds(txtbox) {
    var s = txtbox.value
    //var reg = new RegExp("^((([0]?[1-9]|1[0-2])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?( )?(AM|am|aM|Am|PM|pm|pM|Pm))|(([0]?[0-9]|1[0-9]|2[0-3])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?))$");
    //var reg = new RegExp("^((([0]?[1-9]|1[0-2])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?( )?(AM|am|aM|Am|PM|pm|pM|Pm))|(([0]?[0-9]|1[0-9]|2[0-3])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?))$");

    // var reg = new RegExp("^([1-9]|1[0-2]|0[1-9]){1}(:[0-5][0-9](\:[0-5][0-9]\s?[aApP][mM])){1}$");
    var reg = new RegExp("^(1[0-2]|[1-9]):[0-5][0-9]:[0-5][0-9][ ]?[aApP][mM]$");

    if ((/^\s*$/)
        .test(s)) {} else {
        if (String(s)
            .search(reg) == -1) {
   = "red";
            alert("The time format is incorrect.\r\n\r\nThe correct format is:\r\nHH:MM:SS am/pm\r\n\r\nFor example:\r\n11:41:55 pm");
        } else {
   = "black";

This url shows all date regular expressions:

Maybe shouldn’t include in this topic, the following code shows regular expression usage…..

Public Shared Function CheckDateByRegularExpression(ByVal xxxdatestring As String) As Integer
   ' the time format should be 12-SEP-2012
   ' 0: not empty string, correct time format  
   ' -1: not emtpy string, wrong date format 
   ' 1: empty string  
   If String.IsNullOrEmpty(xxxdatestring) Then
            Return 1
      Dim reg As New Regex("^((31(?! (FEB|APR|JUN|SEP|NOV)))|((30|29)(?! FEB))|(29(?= FEB (((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00)))))|(0?[1-9])|1\d|2[0-8])-(JAN|FEB|MAR|MAY|APR|JUL|JUN|AUG|OCT|SEP|NOV|DEC)-((1[6-9]|[2-9]\d)\d{2})$")
      If reg.IsMatch(xxxdatestring) Then
         Return 0
          Return -1
       End If

    End If
End Function

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s