Spent hours, can't figure out. Javascript dynamic form variable with asp.net
-
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
-
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
try replacing document.Registring.elements[fieldName].value with document.getElementByID(fieldName).value
-
try replacing document.Registring.elements[fieldName].value with document.getElementByID(fieldName).value
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/)
-
try replacing document.Registring.elements[fieldName].value with document.getElementByID(fieldName).value
-
Thanks for the response. I am getting an error in firefox: Error: document.getElementByID is not a function
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.
-
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.
-
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?
OK, sorry - my bad - should be lower-case d at teh end: getElementById (case-sensitive)
-
OK, sorry - my bad - should be lower-case d at teh end: getElementById (case-sensitive)