Forum Moderators: open
var counter = 0;
function moreFields() {
counter++;
var newFields = document.getElementById('readroot').cloneNode(true);
newFields.id = '';
newFields.style.display = 'block';
var newField = newFields.childNodes;
for (var i=0;i<newField.length;i++) {
var theName = newField[i].name
if (theName)
newField[i].name = theName + counter;
}
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
}
window.onload = moreFields;
<span id="sprytextfield1">
<input type="text" name="mytextfield" id="mytextfield"/>
</span>
<script type="text/javascript">
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");
</script>
<div id="standardform" style="display:none">
<input type="hidden" name="counter" value="marker" />
<select name="payment_type">
<option value="single" selected="selected">Single</option>
<option value="1">Monthly</option>
<option value="3">Quarterly</option>
<option value="6">Semi Anual</option>
<option value="12">Anual</option>
</select>
<input name="number" value="" />
<input name="value" value="" />
<input type="date" name="startdate" value="" />
<input type="button" value="Remove Payment"
onclick="this.parentNode.parentNode.removeChild(this.parentNode);" /><br />
</div>
<form method="post" action="formprocess.php">
<span id="writeroot"></span>
<input type="button" id="btnmoreFields" value="Add Payment!" onclick="moreFields();" />
<input type="submit" value="Send" />
</form>
<script>
var counter = 0;
function moreFields() {
counter++;
var newFields = document.getElementById('standardform').cloneNode(true);
newFields.id = '';
newFields.style.display = 'block';
var newField = newFields.childNodes;
for (var i=0;i<newField.length;i++) {
var theName = newField[i].name
if (theName)
newField[i].name = theName + counter;
}
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
}
window.onload = moreFields;
</script>
<input type="date" name="startdate" value="" />
<script>
$(":date").dateinput();
</script>
the jQuery tool only works on the standard form fields, and not on the additionally created ones
"If there is only one date field involved you can hard code it eg..."
<INPUT class=date type=date name=mydate jQuery1291758529453="3">
<script>
$(":date").dateinput();
</script>
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="http://static.flowplayer.org/tools/demos/dateinput/css/skin1.css"/>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
<title>Test</title>
</head>
<div id="standardform" style="display:none">
<input type="hidden" name="counter" value="marker" />
<select name="payment_type">
<option value="single" selected="selected">Single</option>
<option value="1">Monthly</option>
<option value="3">Quarterly</option>
<option value="6">Semi Anual</option>
<option value="12">Anual</option>
</select>
<span id="spryvalidatorNumber">
<input name="number" value="" />
</span>
<span id="spryvalidatorValue">
<input name="value" value="" />
</span>
<span id="spryvalidatorStartDate">
<input type="date" name="startdate" value="" />
</span>
<input type="button" value="Remove Payment"
onclick="this.parentNode.parentNode.removeChild(this.parentNode);" /><br />
</div>
<form method="post" action="formprocess.php">
<div id="writeroot"></div>
<input type="button" id="btnmoreFields" value="Add Payment!" onclick="moreFields();" />
<input type="submit" value="Send" />
</form>
<script src="dateinput.js"></script> <!--http://cdn.jquerytools.org/1.2.5/full/jquery.tools.js?foo"-->
<script src="SpryValidationTextField.js" type="text/javascript"></script>
<script type="text/javascript">
var counter = 0;
var spryValidators = {
textFields: []
};
function updateChildren(el, uniqueValue, validatorId) {
var i,
n,
newFields = el.childNodes,
theName,
uid;
for (i = 0, n = newFields.length; i < n; i++) {
if (newFields[i].nodeType == 1) { // elements
if (newFields[i].tagName.toLowerCase() == "span") {
// give it a unique id
uid = newFields[i].id + "_" + uniqueValue + "_" + i;
newFields[i].id = uid;
// update the child nodes
updateChildren(newFields[i], uid, uid);
}
else {
// Record the textField validator
if (validatorId) {
spryValidators.textFields.push(validatorId);
}
// give it a unique name
theName = newFields[i].name;
if (theName) {
newFields[i].name = theName + counter;
}
}
}
}
}
function moreFields() {
counter++;
// Get the block containing the nodes to be cloned
var newFields = document.getElementById('standardform').cloneNode(true);
newFields.id = '';
newFields.style.display = 'block';
//var newField = newFields.childNodes;
updateChildren(newFields, counter);
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
// Attach spry Validation
for (i = 0, n = spryValidators.textFields.length; i < n; i++) {
if (spryValidators.textFields[i]) {
// This is just for debugging
alert('creating validator: ' + spryValidators.textFields[i]);
new Spry.Widget.ValidationTextField(spryValidators.textFields[i]);
spryValidators.textFields[i] = null;
}
}
// Setup date field
$(":date").each(function (i, el) {
$(this).dateinput();
});
}
</script>
</body>
</html>
// Setup date field
$(":date").each(function (i, el) {
$(this).dateinput();
});
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="http://static.flowplayer.org/tools/demos/dateinput/css/skin1.css"/>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
<title>Test</title>
</head>
<div id="standardform" style="display:none">
<input type="hidden" name="counter" value="marker" />
<select name="payment_type">
<option value="single" selected="selected">Single</option>
<option value="1">Monthly</option>
<option value="3">Quarterly</option>
<option value="6">Semi Anual</option>
<option value="12">Anual</option>
</select>
<span id="spryvalidatorNumber">
<input name="number" value="" />
</span>
<span id="spryvalidatorValue">
<input name="value" value="" />
</span>
<span id="spryvalidatorStartDate">
<input type="date" name="startdate" value="" />
</span>
<input type="button" value="Remove Payment"
onclick="this.parentNode.parentNode.removeChild(this.parentNode);" /><br />
</div>
<form method="post" action="formprocess.php">
<div id="writeroot"></div>
<input type="button" id="btnmoreFields" value="Add Payment!" onclick="moreFields();" />
<input type="submit" value="Send" />
</form>
<script src="dateinput.js"></script> <!--http://cdn.jquerytools.org/1.2.5/full/jquery.tools.js?foo"-->
<script src="SpryValidationTextField.js" type="text/javascript"></script>
<script type="text/javascript">
var counter = 0;
var spryValidators = {
textFields: []
};
/**
* @param el The element whose children we want to inspect
* @param uniqueValue A unique value that will be appended to the id of child
* elements.
* is needed to create the Spry.Widget.ValidationTextField.
*/
function updateChildren(el, uniqueValue) {
var i, // index to an array
n, // length of the array
newFields = el.childNodes, // The children of the current el
theName, // the name attribute of child inputs
uid; // a unique id of the validator span wrapper around an input
// iterate through each of the children of the element passed in
for (i = 0, n = newFields.length; i < n; i++) {
// ignore all children that are not HTMLElement nodes (nodeType 1)
if (newFields[i].nodeType == 1) {
// check to see if the current element is a span element. If so,
// we'll need to updated that elements children as well.
if (newFields[i].tagName.toLowerCase() == "span") {
// give it a unique id based on it's current id, plus the
// unique value that was passed in, plus the index in the array
uid = newFields[i].id + "_" + uniqueValue + "_" + i;
newFields[i].id = uid;
// Record the id value in an array. When we're all done looping,
// we'll create a validator for each item in the array.
spryValidators.textFields.push(uid);
// update the child nodes of the span
updateChildren(newFields[i], uid);
}
else {
// this is not a span element, so must be a form control
// Note, that is not a strict requirement... if the markup that
// we copied contained other elements than form controls and
// span element, then we should probably add additional checks
// here. However, given the HTML above, this should work.
// ...
// give this element a unique name based on the current name
// plus the global counter
theName = newFields[i].name;
if (theName) {
newFields[i].name = theName + counter;
}
}
}
}
}
/**
* Add a duplicate of 'standardform' field items to the form.
*/
function moreFields() {
counter++;
// Get the block containing the nodes to be cloned
var newFields = document.getElementById('standardform').cloneNode(true);
newFields.id = '';
newFields.style.display = 'block';
//var newField = newFields.childNodes;
updateChildren(newFields, counter);
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
// Attach spry Validation
for (i = 0, n = spryValidators.textFields.length; i < n; i++) {
if (spryValidators.textFields[i]) {
// This is just for debugging
alert('creating validator: ' + spryValidators.textFields[i]);
new Spry.Widget.ValidationTextField(spryValidators.textFields[i]);
// remove this field from the array so we dont attempt to create
// it again next time moreFields is called.
spryValidators.textFields[i] = null;
}
}
// Setup date field
var d = $(":date");
d.each(function (i, el) {
$(this).dateinput();
});
}
</script>
</body>
</html>
<span id="sprytextfield1">
<input name="number" value="" />
<span class="textfieldRequiredMsg">A value is required.</span><span class="textfieldInvalidFormatMsg">Invalid format.</span></span>
creating validator: sprytextfield1_2_5
creating validator: _sprytextfield1_2_5_3
creating validator: _sprytextfield1_2_5_4
When creating another field group... the same problem...
creating validator: sprytextfield1_3_5
creating validator: _sprytextfield1_3_5_3
creating validator: _sprytextfield1_3_5_4
etc...
etc...
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "integer", {validateOn:["change"]});
1. for the spry validation for example, there is a span tag within the initial span tag itself as shown below...
2. does the script that you have provided add the relative spry validator script as show below?:-
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="http://static.flowplayer.org/tools/demos/dateinput/css/skin1.css">
<link href="SpryValidationTextFields.css" rel="stylesheet" type="text/css" >
<title>Test</title>
</head>
<div id="standardform" style="display:none">
<input type="hidden" name="counter" value="marker" />
<select name="payment_type">
<option value="single" selected="selected">Single</option>
<option value="1">Monthly</option>
<option value="3">Quarterly</option>
<option value="6">Semi Anual</option>
<option value="12">Anual</option>
</select>
<span id="spryvalidatorNumber">
<input name="number" value="" />
<span class="textfieldRequiredMsg">A value is required.</span>
<span class="textfieldInvalidFormatMsg">Invalid format.</span>
</span>
<span id="spryvalidatorValue">
<input name="value" value="" />
</span>
<span id="spryvalidatorStartDate">
<input type="date" name="startdate" value="" />
</span>
<input type="button" value="Remove Payment"
onclick="this.parentNode.parentNode.removeChild(this.parentNode);" /><br />
</div>
<form method="post" action="formprocess.php">
<div id="writeroot"></div>
<input type="button" id="btnmoreFields" value="Add Payment!" onclick="moreFields();" />
<input type="submit" value="Send" />
</form>
<script src="dateinput.js"></script> <!--http://cdn.jquerytools.org/1.2.5/full/jquery.tools.js?foo"-->
<script src="SpryValidationTextField.js" type="text/javascript"></script>
<script type="text/javascript">
var counter = 0;
var spryValidators = {
textFields: []
};
/**
* @param el The element whose children we want to inspect
* @param uniqueValue A unique value that will be appended to the id of child
* elements.
* is needed to create the Spry.Widget.ValidationTextField.
*/
function updateChildren(el, uniqueValue) {
var i, // index to an array
n, // length of the array
newFields = el.childNodes, // The children of the current el
theName, // the name attribute of child inputs
uid; // a unique id of the validator span wrapper around an input
// iterate through each of the children of the element passed in
for (i = 0, n = newFields.length; i < n; i++) {
// ignore all children that are not HTMLElement nodes (nodeType 1)
if (newFields[i].nodeType == 1) {
// check to see if the current element is a span element. If so,
// we'll need to updated that elements children as well.
if (newFields[i].tagName.toLowerCase() == "span") {
var inputChildren = newFields[i].getElementsByTagName('input');
if (inputChildren && inputChildren.length > 0) {
// give it a unique id based on it's current id, plus the
// unique value that was passed in, plus the index in the array
uid = newFields[i].id + "_" + uniqueValue + "_" + i;
newFields[i].id = uid;
// Record the id value in an array. When we're all done looping,
// we'll create a validator for each item in the array.
spryValidators.textFields.push(uid);
// update the child nodes of the span
updateChildren(newFields[i], uid);
}
}
else {
// this is not a span element, so must be a form control
// Note, that is not a strict requirement... if the markup that
// we copied contained other elements than form controls and
// span element, then we should probably add additional checks
// here. However, given the HTML above, this should work.
// ...
// give this element a unique name based on the current name
// plus the global counter
theName = newFields[i].name;
if (theName) {
newFields[i].name = theName + counter;
}
}
}
}
}
/**
* Add a duplicate of 'standardform' field items to the form.
*/
function moreFields() {
counter++;
// Get the block containing the nodes to be cloned
var newFields = document.getElementById('standardform').cloneNode(true);
newFields.id = '';
newFields.style.display = 'block';
//var newField = newFields.childNodes;
updateChildren(newFields, counter);
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
// Attach spry Validation
for (i = 0, n = spryValidators.textFields.length; i < n; i++) {
if (spryValidators.textFields[i]) {
// This is just for debugging
alert('creating validator: ' + spryValidators.textFields[i]);
new Spry.Widget.ValidationTextField(spryValidators.textFields[i], "integer", {validateOn: ["change"]});
// remove this field from the array so we dont attempt to create
// it again next time moreFields is called.
spryValidators.textFields[i] = null;
}
}
// Setup date field
var d = $(":date");
d.each(function (i, el) {
$(this).dateinput();
});
}
</script>
</body>
</html>
// Setup date field
var d = $(":date");
d.each(function (i, el) {
$(this).dateinput({
format: 'yyyy-mm-dd',// the format displayed for the user
selectors: true, // whether month/year dropdowns are shown
//min: -100, // min selectable day (100 days backwards)
//max: 1, // max selectable day (100 days onwards)
offset: [10, 20], // tweak the position of the calendar
speed: 'fast', // calendar reveal speed
firstDay: 0, // which day starts a week. 0 = sunday, 1 = monday etc..
yearRange: [-6, 7],
trigger: true
});
});
for the <input name="number"...> I wanted a spry validation with the integer type...
for the <input name="value" ...> I wanted the spry validation with the real type.
and finally for the <input type="date" name="startdate" ...> I wanted the spry validation with the date type "yyyy-mm-dd"
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "integer", {validateOn:["change"]});
var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "real", {validateOn:["change"]});
var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3", "date", {format:"yyyy-mm-dd", validateOn:["change"]});
if (inputChildren && inputChildren.length > 0) {
// give it a unique id based on it's current id, plus the
// unique value that was passed in, plus the index in the array
uid = newFields[i].id + "_" + uniqueValue + "_" + i;
newFields[i].id = uid;
// Record the id value in an array. When we're all done looping,
// we'll create a validator for each item in the array.
spryValidators.textFields.push(uid);
// update the child nodes of the span
updateChildren(newFields[i], uid);
}
if (inputChildren && inputChildren.length > 0) {
// give it a unique id based on it's current id, plus the
// unique value that was passed in, plus the index in the array
uid = newFields[i].id + "_" + uniqueValue + "_" + i;
newFields[i].id = uid;
// Look at the child input to determine which type
switch (inputChildren[0].name) {
case "number":
type = "integer";
break;
case "value":
type = "real";
break;
case "startdate":
type = "date";
break;
}
// Record the id value in an array. When we're all done looping,
// we'll create a validator for each item in the array.
spryValidators.textFields.push({uid: uid, type: type});
// update the child nodes of the span
updateChildren(newFields[i], uid);
}
for (i = 0, n = spryValidators.textFields.length; i < n; i++) {
if (spryValidators.textFields[i]) {
// This is just for debugging
alert('creating validator: ' + spryValidators.textFields[i]);
if (spryValidators.textFields[i].type == "number") {
new Spry.Widget.ValidationTextField(spryValidators.textFields[i], "integer", {validateOn: ["change"]});
}
else if (spryValidators.textFields[i].type == "real") {
// create the appropriate validator here...
}
// and if type is startdate, yada yada yada...
// remove this field from the array so we dont attempt to create
// it again next time moreFields is called.
spryValidators.textFields[i] = null;
}
}
integerfor (i = 0, n = spryValidators.textFields.length; i < n; i++) {
if (spryValidators.textFields[i]) {
// This is just for debugging
//alert('creating validator: ' + spryValidators.textFields[i]);
if (spryValidators.textFields[i].type == "") {
new Spry.Widget.ValidationTextField(spryValidators.textFields[i], "integer", {validateOn: ["change"]});
}
else if (spryValidators.textFields[i].type == "real") {
new Spry.Widget.ValidationTextField(spryValidators.textFields[i], "real", {validateOn:["change"]});
}
else if (spryValidators.textFields[i].type == "date") {
new Spry.Widget.ValidationTextField(spryValidators.textFields[i], "date", {validateOn:["change"], format:"yyyy-mm-dd"});
}
// and if type is startdate, yada yada yada...
// remove this field from the array so we dont attempt to create
// it again next time moreFields is called.
spryValidators.textFields[i] = null;
}
}
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="http://static.flowplayer.org/tools/demos/dateinput/css/skin1.css">
<link href="SpryValidationTextFields.css" rel="stylesheet" type="text/css" >
<title>Test</title>
</head>
<div id="standardform" style="display:none">
<input type="hidden" name="counter" value="marker" />
<select name="payment_type">
<option value="single" selected="selected">Single</option>
<option value="1">Monthly</option>
<option value="3">Quarterly</option>
<option value="6">Semi Anual</option>
<option value="12">Anual</option>
</select>
<span id="spryvalidatorNumber">
<input name="number" value="0" />
<span class="textfieldRequiredMsg">A value is required.</span>
<span class="textfieldInvalidFormatMsg">Invalid format.</span>
</span>
<span id="spryvalidatorValue">
<input name="value" value="1" />
</span>
<span id="spryvalidatorStartDate">
<input type="date" class="date" name="startdate" value="" />
</span>
<input type="button" value="Remove Payment"
onclick="this.parentNode.parentNode.removeChild(this.parentNode);" /><br />
</div>
<form method="post" action="formprocess.php">
<div id="writeroot"></div>
<input type="button" id="btnmoreFields" value="Add Payment!" onclick="moreFields();" />
<input type="submit" value="Send" />
</form>
<script src="dateinput.js"></script> <!--http://cdn.jquerytools.org/1.2.5/full/jquery.tools.js?foo"-->
<script src="SpryValidationTextField.js" type="text/javascript"></script>
<script type="text/javascript">
var counter = 0;
var spryValidators = {
textFields: []
};
/**
* Given a DOM node, iterate through the children and return the name of the
* first input found. If no inputs are found, return null.
* @param n The Node whose children will be iterated through
*/
function getFirstInputName(n) {
var children = n.childNodes,
i,
n;
for (i = 0, n = children.length; i < n; i++) {
// if a child node is an element (nodeType 1) and has tagName "input"
// then we can stop searching and return the name
if (children[i].nodeType == 1 && children[i].tagName.toLowerCase() == "input") {
return children[i].name;
}
}
// if no input elements were found, return null
return null;
}
/**
* @param el The element whose children we want to inspect
* @param uniqueValue A unique value that will be appended to the id of child
* elements.
* is needed to create the Spry.Widget.ValidationTextField.
*/
function updateChildren(el, uniqueValue) {
var i, // index to an array
n, // length of the array
newFields = el.childNodes, // The children of the current el
theName, // the name attribute of child inputs
uid; // a unique id of the validator span wrapper around an input
// iterate through each of the children of the element passed in
for (i = 0, n = newFields.length; i < n; i++) {
// ignore all children that are not HTMLElement nodes (nodeType 1)
if (newFields[i].nodeType == 1) {
// check to see if the current element is a span element. If so,
// we'll need to updated that elements children as well.
if (newFields[i].tagName.toLowerCase() == "span") {
var firstInputName = getFirstInputName(newFields[i]);
if (firstInputName) {
// give it a unique id based on it's current id, plus the
// unique value that was passed in, plus the index in the array
uid = newFields[i].id + "_" + uniqueValue + "_" + i;
newFields[i].id = uid;
// Record the id value in an array. When we're all done looping,
// we'll create a validator for each item in the array.
spryValidators.textFields.push({uid: uid, name: firstInputName});
// update the child nodes of the span
updateChildren(newFields[i], uid);
}
}
else {
// this is not a span element, so must be a form control
// Note, that is not a strict requirement... if the markup that
// we copied contained other elements than form controls and
// span element, then we should probably add additional checks
// here. However, given the HTML above, this should work.
// ...
// give this element a unique name based on the current name
// plus the global counter
theName = newFields[i].name;
if (theName) {
newFields[i].name = theName + counter;
}
}
}
}
}
/**
* Add a duplicate of 'standardform' field items to the form.
*/
function moreFields() {
counter++;
// Get the block containing the nodes to be cloned
var newFields = document.getElementById('standardform').cloneNode(true);
newFields.id = '';
newFields.style.display = 'block';
//var newField = newFields.childNodes;
updateChildren(newFields, counter);
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
// Attach spry Validation
for (i = 0, n = spryValidators.textFields.length; i < n; i++) {
if (spryValidators.textFields[i]) {
// This is just for debugging
alert('creating validator: ' + spryValidators.textFields[i].uid + ", " +spryValidators.textFields[i].name);
switch (spryValidators.textFields[i].name) {
case "number":
new Spry.Widget.ValidationTextField(spryValidators.textFields[i].uid, "integer", {validateOn: ["change"]});
break;
case "value":
new Spry.Widget.ValidationTextField(spryValidators.textFields[i].uid, "real", {validateOn: ["change"]});
break;
case "startdate":
new Spry.Widget.ValidationTextField(spryValidators.textFields[i].uid, "date", {validateOn: ["change"], format:"yyyy-mm-dd"});
break;
}
// remove this field from the array so we dont attempt to create
// it again next time moreFields is called.
spryValidators.textFields[i] = null;
}
}
// Setup date field
var d = $("input.date");
d.each(function (i, el) {
$(this).dateinput();
});
}
</script>
</body>
</html>
var d = $("input.date");
d.each(function (i, el) {
alert("CALLING DATE FUNCTION");
$(this).dateinput();
});
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="http://static.flowplayer.org/tools/demos/dateinput/css/skin1.css">
<link href="SpryValidationTextFields.css" rel="stylesheet" type="text/css" >
<title>Test</title>
</head>
<div id="standardform" style="display:none">
<input type="hidden" name="counter" value="marker" />
<select name="payment_type">
<option value="single" selected="selected">Single</option>
<option value="1">Monthly</option>
<option value="3">Quarterly</option>
<option value="6">Semi Anual</option>
<option value="12">Anual</option>
</select>
<span id="spryvalidatorNumber">
<input name="number" value="0" />
<span class="textfieldRequiredMsg">A value is required.</span>
<span class="textfieldInvalidFormatMsg">Invalid format.</span>
</span>
<span id="spryvalidatorValue">
<input name="value" value="1" />
</span>
<span id="spryvalidatorStartDate">
<input type="date" class="date" name="startdate" value="" />
</span>
<input type="button" value="Remove Payment"
onclick="this.parentNode.parentNode.removeChild(this.parentNode);" /><br />
</div>
<form method="post" action="formprocess.php">
<div id="writeroot"></div>
<input type="button" id="btnmoreFields" value="Add Payment!" onclick="moreFields();" />
<input type="submit" value="Send" />
</form>
<script src="dateinput.js"></script> <!--http://cdn.jquerytools.org/1.2.5/full/jquery.tools.js?foo"-->
<script src="SpryValidationTextField.js" type="text/javascript"></script>
<script type="text/javascript">
var counter = 0;
var spryValidators = {
textFields: []
};
/**
* Given a DOM node, iterate through the children and return the name of the
* first input found. If no inputs are found, return null.
* @param n The Node whose children will be iterated through
*/
function getFirstInputName(n) {
var children = n.childNodes,
i,
n;
for (i = 0, n = children.length; i < n; i++) {
// if a child node is an element (nodeType 1) and has tagName "input"
// then we can stop searching and return the name
if (children[i].nodeType == 1 && children[i].tagName.toLowerCase() == "input") {
return children[i].name;
}
}
// if no input elements were found, return null
return null;
}
/**
* @param el The element whose children we want to inspect
* @param uniqueValue A unique value that will be appended to the id of child
* elements.
* is needed to create the Spry.Widget.ValidationTextField.
*/
function updateChildren(el, uniqueValue) {
var i, // index to an array
n, // length of the array
newFields = el.childNodes, // The children of the current el
theName, // the name attribute of child inputs
uid; // a unique id of the validator span wrapper around an input
// iterate through each of the children of the element passed in
for (i = 0, n = newFields.length; i < n; i++) {
// ignore all children that are not HTMLElement nodes (nodeType 1)
if (newFields[i].nodeType == 1) {
// check to see if the current element is a span element. If so,
// we'll need to updated that elements children as well.
if (newFields[i].tagName.toLowerCase() == "span") {
var firstInputName = getFirstInputName(newFields[i]);
if (firstInputName) {
// give it a unique id based on it's current id, plus the
// unique value that was passed in, plus the index in the array
uid = newFields[i].id + "_" + uniqueValue + "_" + i;
newFields[i].id = uid;
// Record the id value in an array. When we're all done looping,
// we'll create a validator for each item in the array.
spryValidators.textFields.push({uid: uid, name: firstInputName});
// update the child nodes of the span
updateChildren(newFields[i], uid);
}
}
else {
// this is not a span element, so must be a form control
// Note, that is not a strict requirement... if the markup that
// we copied contained other elements than form controls and
// span element, then we should probably add additional checks
// here. However, given the HTML above, this should work.
// ...
// give this element a unique name based on the current name
// plus the global counter
theName = newFields[i].name;
if (theName) {
newFields[i].setAttribute("name", theName + counter);
newFields[i].name = theName + counter;
}
}
}
}
}
/**
* Add a duplicate of 'standardform' field items to the form.
*/
function moreFields() {
counter++;
// Get the block containing the nodes to be cloned
var newFields = document.getElementById('standardform').cloneNode(true);
newFields.id = '';
newFields.style.display = 'block';
//var newField = newFields.childNodes;
updateChildren(newFields, counter);
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
// Attach spry Validation
for (i = 0, n = spryValidators.textFields.length; i < n; i++) {
if (spryValidators.textFields[i]) {
// This is just for debugging
alert('creating validator: ' + spryValidators.textFields[i].uid + ", " +spryValidators.textFields[i].name);
switch (spryValidators.textFields[i].name) {
case "number":
new Spry.Widget.ValidationTextField(spryValidators.textFields[i].uid, "integer", {validateOn: ["change"]});
break;
case "value":
new Spry.Widget.ValidationTextField(spryValidators.textFields[i].uid, "real", {validateOn: ["change"]});
break;
case "startdate":
new Spry.Widget.ValidationTextField(spryValidators.textFields[i].uid, "date", {validateOn: ["change"], format:"yyyy-mm-dd"});
break;
}
// remove this field from the array so we dont attempt to create
// it again next time moreFields is called.
spryValidators.textFields[i] = null;
}
}
// Setup date field
var d = $("input.date[name!=startdate]");
d.each(function (i, el) {
$(el).dateinput();
});
}
</script>
</body>
</html>
A Value is Required
new Spry.Widget.ValidationTextField(spryValidators.textFields[i].uid, "date", {validateOn: ["change"], format:"yyyy-mm-dd"});
new Spry.Widget.ValidationTextField(spryValidators.textFields[i].uid, "date", {validateOn: ["change"], format:"mm/dd/yy"});
var d = $("input.date[name!=startdate]");
d.each(function (i, el) {
$(el).dateinput(
{
format: 'yyyy-mm-dd',// the format displayed for the user
selectors: true, // whether month/year dropdowns are shown
//min: -100, // min selectable day (100 days backwards)
//max: 1, // max selectable day (100 days onwards)
offset: [10, 20], // tweak the position of the calendar
speed: 'fast', // calendar reveal speed
firstDay: 0, // which day starts a week. 0 = sunday, 1 = monday etc..
yearRange: [-6, 7],
trigger: true
}
);
});
<!DOCTYPE html>
<head>
<link rel="stylesheet" type="text/css" href="http://static.flowplayer.org/tools/demos/dateinput/css/skin1.css">
<link href="SpryValidationTextFields.css" rel="stylesheet" type="text/css" >
<title>Test</title>
</head>
<div id="standardform" style="display:none">
<input type="hidden" name="counter" value="marker" />
<select name="payment_type">
<option value="single" selected="selected">Single</option>
<option value="1">Monthly</option>
<option value="3">Quarterly</option>
<option value="6">Semi Anual</option>
<option value="12">Anual</option>
</select>
<span id="spryvalidatorNumber">
<input name="number" value="0" />
<span class="textfieldRequiredMsg">A value is required.</span>
<span class="textfieldInvalidFormatMsg">Invalid format.</span>
</span>
<span id="spryvalidatorValue">
<input name="value" value="1" />
</span>
<span id="spryvalidatorStartDate">
<input type="date" class="date" name="startdate" value="" />
</span>
<input type="button" value="Remove Payment"
onclick="this.parentNode.parentNode.removeChild(this.parentNode);" /><br />
</div>
<form method="post" action="formprocess.php">
<div id="writeroot"></div>
<input type="button" id="btnmoreFields" value="Add Payment!" onclick="moreFields();" />
<input type="submit" value="Send" />
</form>
<script src="dateinput.js"></script> <!--http://cdn.jquerytools.org/1.2.5/full/jquery.tools.js?foo"-->
<script src="SpryValidationTextField.js" type="text/javascript"></script>
<script type="text/javascript">
var counter = 0;
var spryValidators = {
textFields: []
};
/**
* Given a DOM node, iterate through the children and return the name of the
* first input found. If no inputs are found, return null.
* @param n The Node whose children will be iterated through
*/
function getFirstInputName(n) {
var children = n.childNodes,
i,
n;
for (i = 0, n = children.length; i < n; i++) {
// if a child node is an element (nodeType 1) and has tagName "input"
// then we can stop searching and return the name
if (children[i].nodeType == 1 && children[i].tagName.toLowerCase() == "input") {
return children[i].name;
}
}
// if no input elements were found, return null
return null;
}
/**
* @param el The element whose children we want to inspect
* @param uniqueValue A unique value that will be appended to the id of child
* elements.
* is needed to create the Spry.Widget.ValidationTextField.
*/
function updateChildren(el, uniqueValue) {
var i, // index to an array
n, // length of the array
newFields = el.childNodes, // The children of the current el
theName, // the name attribute of child inputs
uid; // a unique id of the validator span wrapper around an input
// iterate through each of the children of the element passed in
for (i = 0, n = newFields.length; i < n; i++) {
// ignore all children that are not HTMLElement nodes (nodeType 1)
if (newFields[i].nodeType == 1) {
// check to see if the current element is a span element. If so,
// we'll need to updated that elements children as well.
if (newFields[i].tagName.toLowerCase() == "span") {
var firstInputName = getFirstInputName(newFields[i]);
if (firstInputName) {
// give it a unique id based on it's current id, plus the
// unique value that was passed in, plus the index in the array
uid = newFields[i].id + "_" + uniqueValue + "_" + i;
newFields[i].id = uid;
// Record the id value in an array. When we're all done looping,
// we'll create a validator for each item in the array.
spryValidators.textFields.push({uid: uid, name: firstInputName});
// update the child nodes of the span
updateChildren(newFields[i], uid);
}
}
else {
// this is not a span element, so must be a form control
// Note, that is not a strict requirement... if the markup that
// we copied contained other elements than form controls and
// span element, then we should probably add additional checks
// here. However, given the HTML above, this should work.
// ...
// give this element a unique name based on the current name
// plus the global counter
theName = newFields[i].name;
if (theName) {
newFields[i].setAttribute("name", theName + counter);
newFields[i].name = theName + counter;
}
}
}
}
}
/**
* Add a duplicate of 'standardform' field items to the form.
*/
function moreFields() {
counter++;
// Get the block containing the nodes to be cloned
var newFields = document.getElementById('standardform').cloneNode(true);
newFields.id = '';
newFields.style.display = 'block';
//var newField = newFields.childNodes;
updateChildren(newFields, counter);
var insertHere = document.getElementById('writeroot');
insertHere.parentNode.insertBefore(newFields,insertHere);
// Setup date field
var d = $("input.date[name!=startdate]");
d.each(function (i, el) {
$(el).dateinput({
format: 'yyyy-mm-dd', // the format displayed for the user
selectors: true, // whether month/year dropdowns are shown
//min: -100, // min selectable day (100 days backwards)
//max: 1, // max selectable day (100 days onwards)
offset: [10, 20], // tweak the position of the calendar
speed: 'fast', // calendar reveal speed
firstDay: 0, // which day starts a week. 0 = sunday, 1 = monday etc..
yearRange: [-6, 7],
trigger: true
});
});
// Attach spry Validation
for (i = 0, n = spryValidators.textFields.length; i < n; i++) {
if (spryValidators.textFields[i]) {
// This is just for debugging
alert('creating validator: ' + spryValidators.textFields[i].uid + ", " +spryValidators.textFields[i].name);
switch (spryValidators.textFields[i].name) {
case "number":
new Spry.Widget.ValidationTextField(spryValidators.textFields[i].uid, "integer", {validateOn: ["change"]});
break;
case "value":
new Spry.Widget.ValidationTextField(spryValidators.textFields[i].uid, "real", {validateOn: ["change"]});
break;
case "startdate":
new Spry.Widget.ValidationTextField(spryValidators.textFields[i].uid, "date", {validateOn: ["change"], format:"yyyy-mm-dd"});
break;
}
// remove this field from the array so we dont attempt to create
// it again next time moreFields is called.
spryValidators.textFields[i] = null;
}
}
}
</script>
</body>
</html>