Validation Control, no error, but it doesn't work properly

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Omg83191
    New Member
    • Aug 2012
    • 21

    Validation Control, no error, but it doesn't work properly

    here is my code, for all the validation control
    ASP.net
    Code:
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" 
                ControlToValidate="txtName" ErrorMessage="Email is required." 
                SetFocusOnError="True">*</asp:RequiredFieldValidator>
    
    <asp:RegularExpressionValidator ID="RegularExpressionValidator9" runat="server" 
                ControlToValidate="txtName" 
                ErrorMessage="Please input your real email address." 
                ValidationExpression=".*@.*\..*">abc@xyz.com</asp:RegularExpressionValidator>
    
    
    
    <asp:RequiredFieldValidator ID="RequiredFieldValidator17" runat="server" 
                ControlToValidate="txtPassword" ErrorMessage="Password is required." 
                SetFocusOnError="True" Display="Dynamic">*</asp:RequiredFieldValidator>
    etc....


    and here is my code for my mysql connection
    VB.net

    Code:
    Imports MySql.Data.MySqlClient
    Partial Class _Default  
        Inherits System.Web.UI.Page
        Dim ServerString As String = "Server=localhost;User Id=root;Password=**;Database=aa"
        Dim SQLConnection As MySqlConnection = New MySqlConnection
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
    
            SQLConnection.ConnectionString = ServerString
    
            Try
                If SQLConnection.State = Data.ConnectionState.Closed Then
                    SQLConnection.Open()
                    MsgBox("Succesfully connected to MySQL database!")
                Else
                    SQLConnection.Close()
                    MsgBox("Connection is closed!")
    
                End If
            Catch ex As Exception
                MsgBox(ex.ToString)
    
            End Try
        End Sub
    
        Protected Sub cmdSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSave.Click
    
            
    
            Dim SQLStatement As String = "INSERT INTO tbluserinfo(userName,userPassword,userFname,userMname,userLname,userBmonth,userBday,userByear,userGender,userCstatus,userCitizenship,userCountry,userOccupation,userSquestion,userSanswer) VALUES('" & txtName.Text & "', '" & txtPassword.Text & "', '" & txtFname.Text & "', '" & txtMname.Text & "', '" & txtLname.Text & "','" & cmbBmonth.Text & "','" & cmbBday.Text & "','" & cmbByear.Text & "','" & cmbGender.Text & "','" & cmbCstatus.Text & "','" & txtCitizenship.Text & "','" & txtCountry.Text & "','" & txtOccupation.Text & "','" & cmbSquestion.Text & "','" & txtSanswer.Text & "' )"
            SaveNames(SQLStatement)
    
    
        End Sub
    
        Public Sub SaveNames(ByRef SQLStatement As String)
            Dim cmd As MySqlCommand = New MySqlCommand
    
            With cmd
                .CommandText = SQLStatement
                .CommandType = Data.CommandType.Text
                .Connection = SQLConnection
                .ExecuteNonQuery()
    
            End With
    
            SQLConnection.Close()
            MsgBox("Succesfully Added!")
            SQLConnection.Dispose()
        End Sub
    
    
    End Class
    The thing is sometimes it works properly, then sometimes not. Then after clicking Save, I'm gonna check my database then it has the data.
    Last edited by Omg83191; Aug 28 '12, 05:18 AM. Reason: Additional Code
  • PsychoCoder
    Recognized Expert Contributor
    • Jul 2010
    • 465

    #2
    Ok since you have 2 validation controls on txtName when it doesn't work what error message is displayed (because both validators have different error messages)

    Comment

    • PsychoCoder
      Recognized Expert Contributor
      • Jul 2010
      • 465

      #3
      This is a far better regular expression pattern to validate an email address (I would know because I wrote it ;) )

      Code:
      @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"

      Comment

      • Omg83191
        New Member
        • Aug 2012
        • 21

        #4
        It doesn't work, actually i have 16 requiredfieldva lidator and 6 regular expression.

        Comment

        • PsychoCoder
          Recognized Expert Contributor
          • Jul 2010
          • 465

          #5
          Do you know what part is failing?

          Comment

          • Omg83191
            New Member
            • Aug 2012
            • 21

            #6
            .*@.*\..* - I use this for email validation, it works
            For confirmation of password, i use the compare validator it works. But the rest of the required field validator doesn't work, I am able to send data to my mysql database even though other fields are empty. Then after saving the data, suddenly the validators will work.. though the regular expression doesn't work

            Comment

            • Omg83191
              New Member
              • Aug 2012
              • 21

              #7
              ^[a-zA-Z]+$+_ i use this so that users can only input letters

              Comment

              Working...