By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,995 Members | 1,090 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,995 IT Pros & Developers. It's quick & easy.

error validating dynamic forms

P: n/a
I have a form in which I am generating dynamic fields. I can do this
fine, but when I go to validate them there are errors. The non-
dynamic fields validate correctly. Below is the html and js. Can
someone explain why the dynamic field is failing, and what I can do to
correct it? I can tell as much that the validation fails when
testing: "if (!elem.firstChild)"

<html>
<head>
<script type="text/javascript" src="dynamicFields.js"></script>
<script type="text/javascript" src="validateScripts.js"></script>
<style>
.info { color: black; background-color: transparent; font-weight:
normal;}
.warn { color: rgb(120,0,0); background-color: transparent; font-
weight: bold;}
.error { color: red; background-color: transparent; font-weight:
bold;}
</style>
</head>

<body>
<input type=text name=number size=3 value=1>
<input type=button value="update" onClick="addField(number.value);">

<br>
<table>
<tr>
<td>Below field will <b>NOT</bvalidate correctly</td>
</tr>
<tr>
<span id=fieldHolder style="position:relative;"></span>
</tr>
</table>

<table>
<tr>
<td>Below field will validate correctly</td><td></td>
</tr>
<tr>
<td><input type="text" id="works"
onchange="validate(this,'inf_works');"></td>
<td id="inf_works">&nbsp;</td>
</tr>
</table>

</body>
</html>

======= validateScripts.js =======
var nbsp = 160;
var node_text = 3;
var emptyString = /^\s*$/ ;

function msg (fld, msgType, message)
{
var elem = document.getElementById(fld);
elem.firstChild.nodeValue = message;

elem.className = msgType;
}

function validate(valField,infoField)
{
var elem = document.getElementById(infoField);
alert('a');
if (!elem.firstChild) return true;
alert('b');
if (elem.firstChild.nodeType != node_text) return true;
alert('c');

valField.value = valField.value.toUpperCase();

if (emptyString.test(valField.value))
{
msg(infoField,"error","No Data?");
return false;
}
else
{
msg(infoField,"warn","WARN: some data changed");
}

return true;
}
======dynamicFields.js ======
function addField(number)
{
var data;

data = '<td><input type="text" id="notWork" onChange="validate(this,
\'inf_notWork\');"></td>';
data = data + '<td id="inf_notWork">&nbsp;</td>';
fieldHolder.innerHTML = data;
}

May 4 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Can you not perform .firstChild on a dynamic object? I know the
object exists, and if I play with the validate function, I can see the
text in the user field. I don't understand why the firstchild of the
label can't be accessed.

May 7 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.