Forum Moderators: open

Message Too Old, No Replies

Javascripts in XSL

Validating an XSL HTML form

         

Alina

9:48 pm on May 24, 2005 (gmt 0)

10+ Year Member



Hello

Please could someone advise me on how to create a javascript that can validate a generated XSL/HTML form? Each (survey) form will be different - so the fields are unknown - meaning a hard-encoded javascript will not do.

I enclose the XSL below: Basically i would like to extend the javascript "validate()" to do proper checking to see if a field is being set or not. At the moment all it does is print a general message listing all the questions that need filling.

I have included a subset of the XSL below ...
many thanks for your help - Alina

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="html"/>

<xsl:template match="/readersurvey">
<html>
<head>
<title><xsl:value-of select="description"/></title>
</head>
<script type="text/javascript" language="JavaScript">
function validate()
{
var errortext=''
<xsl:apply-templates select="question" mode="validation"/>
if (errortext!='')
{
alert('Form Incomplete, please:\n'+errortext);
return false;
}
return true;
}
</script>
<body>
<h1><xsl:value-of select="description"/></h1>
<h3><xsl:value-of select="displaymessage" disable-output-escaping="yes"/></h3>
<form action="processor.php" method="post" onsubmit="return validate()" name="surveyform">
<xsl:apply-templates select="question" mode="control"/>
<p><input type="submit" name="sendform" value="submit"/></p>
</form>
</body>
</html>
</xsl:template>

<!-- questions with validation -->

<xsl:template match="question" mode="validation">
<xsl:param name="prefix"></xsl:param>
<xsl:apply-templates select="question" mode="validation">
<xsl:with-param name="prefix"><xsl:value-of select="$prefix"/><xsl:value-of select="position()"/>.</xsl:with-param>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="question[@type='radio' and @mandatory='yes']" mode="validation">
<xsl:param name="prefix"></xsl:param>
errortext=errortext+'Please select a radio reponse to: <xsl:value-of select="$prefix"/><xsl:value-of select="position()"/>.\n';
<xsl:apply-templates select="question" mode="validation">
<xsl:with-param name="prefix"><xsl:value-of select="$prefix"/><xsl:value-of select="position()"/>.</xsl:with-param>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="question[@type='checkbox' and @mandatory='yes']" mode="validation">
<xsl:param name="prefix"></xsl:param>
errortext=errortext+'Please select a checkbox reponse to: <xsl:value-of select="$prefix"/><xsl:value-of select="position()"/>.\n';
<xsl:apply-templates select="question" mode="validation">
<xsl:with-param name="prefix"><xsl:value-of select="$prefix"/><xsl:value-of select="position()"/>.</xsl:with-param>
</xsl:apply-templates>
</xsl:template>

<xsl:template match="question[@type='textarea' and @mandatory='yes']" mode="validation">
<xsl:param name="prefix"></xsl:param>
errortext=errortext+'Please select a textarea reponse to: <xsl:value-of select="$prefix"/><xsl:value-of select="position()"/>.\n';
</xsl:template>

<!-- questions with control -->

<xsl:template match="question[@type='radio']" mode="control">
<xsl:variable name="qcount"><xsl:text>q</xsl:text><xsl:number value="position()" format="1. "/></xsl:variable>
<p><xsl:value-of select="$qcount"/><xsl:value-of select="@text"/></p>
<xsl:apply-templates select="option" mode="oneofmany"/>
</xsl:template>

<!-- options -->

<xsl:template match="option" mode="oneofmany">
<p>
<input type="radio" name="{../@id}" value="{@id}"><xsl:if test="@type='selected'"><xsl:attribute name="checked">checked</xsl:attribute></xsl:if></input>
<xsl:value-of select="@text"/>
</p>
<xsl:apply-templates mode="control"/>
</xsl:template>

<xsl:template match="option" mode="manyofmany">
<p>
<input type="checkbox" name="{../@id}{@id}"><xsl:if test="@type='selected'"><xsl:attribute name="checked">checked</xsl:attribute></xsl:if></input>
<xsl:value-of select="@text"/>
</p>
<xsl:apply-templates mode="control"/>
</xsl:template>

</xsl:stylesheet>