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
  1. Home
  2. Web Development
  3. ASP.NET
  4. Spent hours, can't figure out. Javascript dynamic form variable with asp.net

Spent hours, can't figure out. Javascript dynamic form variable with asp.net

Scheduled Pinned Locked Moved ASP.NET
helpcsharpjavascriptasp-netsysadmin
8 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.
  • A Offline
    A Offline
    Albert83
    wrote on last edited by
    #1

    Hi I have a form with text fields that I am trying to validate. I get this error and validation fails. Firefox error Console shows: Error: document.Registring.elements.fieldName is undefined It doesn't recognize fieldName as variable. Without runat="server" I think the issue disappears. How to fix this? Thank you. Here is the snippet: function checkform(fieldName) { var fieldIs = document.Registring.elements[fieldName].value; if (fieldIs == "") return false; else return true; } function ValidateFields() { var errorMessage = ""; var returnResult = true; //Personal Information if (!checkform('FirstName')) { errorMessage += "First Name is a required field \n"; returnResult = false; } if (!validateInputType('FirstName', "letters")) { errorMessage += "First Name should contain letters only \n"; returnResult = false; } } function validateInputType(fieldName, inputType) { if (inputType == "digits") { var isNumber = document.Registring.elements[fieldName].value; //digits only if (isNumber.match(/^\d+$/)) return true; else return false; } else if (inputType == "letters") { var isLetter = document.Registring.elements[fieldName].value; //letters only if (isLetter.match(/[a-zA-Z]/)) return true; else return false; } } Reservations

    PERSONAL INFORMATION

    N 1 Reply Last reply
    0
    • A Albert83

      Hi I have a form with text fields that I am trying to validate. I get this error and validation fails. Firefox error Console shows: Error: document.Registring.elements.fieldName is undefined It doesn't recognize fieldName as variable. Without runat="server" I think the issue disappears. How to fix this? Thank you. Here is the snippet: function checkform(fieldName) { var fieldIs = document.Registring.elements[fieldName].value; if (fieldIs == "") return false; else return true; } function ValidateFields() { var errorMessage = ""; var returnResult = true; //Personal Information if (!checkform('FirstName')) { errorMessage += "First Name is a required field \n"; returnResult = false; } if (!validateInputType('FirstName', "letters")) { errorMessage += "First Name should contain letters only \n"; returnResult = false; } } function validateInputType(fieldName, inputType) { if (inputType == "digits") { var isNumber = document.Registring.elements[fieldName].value; //digits only if (isNumber.match(/^\d+$/)) return true; else return false; } else if (inputType == "letters") { var isLetter = document.Registring.elements[fieldName].value; //letters only if (isLetter.match(/[a-zA-Z]/)) return true; else return false; } } Reservations

      PERSONAL INFORMATION

      N Offline
      N Offline
      NeverHeardOfMe
      wrote on last edited by
      #2

      try replacing document.Registring.elements[fieldName].value with document.getElementByID(fieldName).value

      R A 2 Replies Last reply
      0
      • N NeverHeardOfMe

        try replacing document.Registring.elements[fieldName].value with document.getElementByID(fieldName).value

        R Offline
        R Offline
        R Giskard Reventlov
        wrote on last edited by
        #3

        and add an id attribute to the item as: ... etc. "If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." **Red Adair**. **nils illegitimus carborundum** [me, me, me](http://www.merrens.com/)

        1 Reply Last reply
        0
        • N NeverHeardOfMe

          try replacing document.Registring.elements[fieldName].value with document.getElementByID(fieldName).value

          A Offline
          A Offline
          Albert83
          wrote on last edited by
          #4

          Thanks for the response. I am getting an error in firefox: Error: document.getElementByID is not a function

          N 1 Reply Last reply
          0
          • A Albert83

            Thanks for the response. I am getting an error in firefox: Error: document.getElementByID is not a function

            N Offline
            N Offline
            NeverHeardOfMe
            wrote on last edited by
            #5

            Well, it is... make sure you have replaced your square brackets [] with round ones (), that your spelling is correct, and that you have also followed the advice of digital man following my earlier post.

            A 1 Reply Last reply
            0
            • N NeverHeardOfMe

              Well, it is... make sure you have replaced your square brackets [] with round ones (), that your spelling is correct, and that you have also followed the advice of digital man following my earlier post.

              A Offline
              A Offline
              Albert83
              wrote on last edited by
              #6

              Thanks. This is the code I have used: var fieldIs = document.getElementByID(fieldName).value; And I have added id as follows: Is that how it should be?

              N 1 Reply Last reply
              0
              • A Albert83

                Thanks. This is the code I have used: var fieldIs = document.getElementByID(fieldName).value; And I have added id as follows: Is that how it should be?

                N Offline
                N Offline
                NeverHeardOfMe
                wrote on last edited by
                #7

                OK, sorry - my bad - should be lower-case d at teh end: getElementById (case-sensitive)

                A 1 Reply Last reply
                0
                • N NeverHeardOfMe

                  OK, sorry - my bad - should be lower-case d at teh end: getElementById (case-sensitive)

                  A Offline
                  A Offline
                  Albert83
                  wrote on last edited by
                  #8

                  Thanks that worked!

                  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