Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. custom javascript validation for form

custom javascript validation for form

Scheduled Pinned Locked Moved ASP.NET
csharpjavascriptasp-netquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    playout
    wrote on last edited by
    #1

    Hi, I am having problems firing a javascript function on an asp.net page. I add the onclick event to the button, but the event does not fire. Here is my code: VB btnSubmit.Attributes.Add("onclick", "val_update(" & Me.drpLocations.ClientID.ToString & "," & Me.drpStatus.ClientID.ToString & "," & Me.txtUserLoginName.ClientID.ToString & "," & Me.Form.ClientID.ToString & ");") ASPX var msgInvalid = "Not validated" function val_update(valLocation, valStatus, valUser, valForm){ if (valStatus.selectedIndex == 2 && valLocation.selectedIndex == 0) { msg = "You need to add a Location" alert(msg); return false; } if (valUser.value = "") { msg = "You need to add a UserName" alert(msg); return false; } else { alert(msgInvalid); return false } } 'button here - normal button with ID, runat command, and text. Every time I click the button, the else statement gets processed, and "Not fired" is alerted. Not sure what I am doing wrong. Is there any way how I can do the above validation client-side?

    G A 2 Replies Last reply
    0
    • P playout

      Hi, I am having problems firing a javascript function on an asp.net page. I add the onclick event to the button, but the event does not fire. Here is my code: VB btnSubmit.Attributes.Add("onclick", "val_update(" & Me.drpLocations.ClientID.ToString & "," & Me.drpStatus.ClientID.ToString & "," & Me.txtUserLoginName.ClientID.ToString & "," & Me.Form.ClientID.ToString & ");") ASPX var msgInvalid = "Not validated" function val_update(valLocation, valStatus, valUser, valForm){ if (valStatus.selectedIndex == 2 && valLocation.selectedIndex == 0) { msg = "You need to add a Location" alert(msg); return false; } if (valUser.value = "") { msg = "You need to add a UserName" alert(msg); return false; } else { alert(msgInvalid); return false } } 'button here - normal button with ID, runat command, and text. Every time I click the button, the else statement gets processed, and "Not fired" is alerted. Not sure what I am doing wrong. Is there any way how I can do the above validation client-side?

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Use the document.getElementById method to get the elements from their id.

      --- b { font-weight: normal; }

      P 1 Reply Last reply
      0
      • G Guffa

        Use the document.getElementById method to get the elements from their id.

        --- b { font-weight: normal; }

        P Offline
        P Offline
        playout
        wrote on last edited by
        #3

        I am not to sure how to do that, as the asp.net gives my textboxes and dropdown boxes different names to the names I specify in the asp.net. EG. My txtfield is called "txtUserLoginName", but when I view source, it's called "ctl00$cphCenter$txtUserLoginName"

        G 1 Reply Last reply
        0
        • P playout

          I am not to sure how to do that, as the asp.net gives my textboxes and dropdown boxes different names to the names I specify in the asp.net. EG. My txtfield is called "txtUserLoginName", but when I view source, it's called "ctl00$cphCenter$txtUserLoginName"

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          playout wrote:

          I am not to sure how to do that, as the asp.net gives my textboxes and dropdown boxes different names to the names I specify in the asp.net.

          But you already do that in the code. The problem is that you are treating the id strings as variable names instead of strings in the Javascript code.

          --- b { font-weight: normal; }

          P 1 Reply Last reply
          0
          • G Guffa

            playout wrote:

            I am not to sure how to do that, as the asp.net gives my textboxes and dropdown boxes different names to the names I specify in the asp.net.

            But you already do that in the code. The problem is that you are treating the id strings as variable names instead of strings in the Javascript code.

            --- b { font-weight: normal; }

            P Offline
            P Offline
            playout
            wrote on last edited by
            #5

            I found the problem, thanks. Got it to work now. Thanks for the help. If you didnt point it out to me, I would probarly still be sitting clueless.

            1 Reply Last reply
            0
            • P playout

              Hi, I am having problems firing a javascript function on an asp.net page. I add the onclick event to the button, but the event does not fire. Here is my code: VB btnSubmit.Attributes.Add("onclick", "val_update(" & Me.drpLocations.ClientID.ToString & "," & Me.drpStatus.ClientID.ToString & "," & Me.txtUserLoginName.ClientID.ToString & "," & Me.Form.ClientID.ToString & ");") ASPX var msgInvalid = "Not validated" function val_update(valLocation, valStatus, valUser, valForm){ if (valStatus.selectedIndex == 2 && valLocation.selectedIndex == 0) { msg = "You need to add a Location" alert(msg); return false; } if (valUser.value = "") { msg = "You need to add a UserName" alert(msg); return false; } else { alert(msgInvalid); return false } } 'button here - normal button with ID, runat command, and text. Every time I click the button, the else statement gets processed, and "Not fired" is alerted. Not sure what I am doing wrong. Is there any way how I can do the above validation client-side?

              A Offline
              A Offline
              Anshuman Singh
              wrote on last edited by
              #6

              you are doing correctaly but you need to be get values in function instead of passing in function. like getElimentbyId('<% drpLocations.ClientID %>') then use it.

              Anshuman Singh [anshumas@rediffmail.com](mailto:Anshuman Singhanshumas@rediffmail.com)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups