xsl 文件中输入字段的 Javascript 验证

标签 javascript jquery xslt

我有以下 xsl:

    <xsl:template match="attribute[controlType='TextBox']">
    <input style="width:180pt" type="input" value="">
        <xsl:attribute name="id">fldCsXML_<xsl:value-of select="name"/></xsl:attribute>
        <xsl:attribute name="name">fldCsXML_<xsl:value-of select="name"/></xsl:attribute>
        <xsl:attribute name="value"><xsl:value-of select="data"/></xsl:attribute>
    </input>
</xsl:template>

我想对值部分进行 javascript 验证以不允许使用符号。只是允许数字或字母。

也许与 jquery 类似:

try bellow script this will not allow special charter # $ % ^ & * ( )

function validate() {
    var element = document.getElementById('input-field');
    element.value = element.value.replace(/[^a-zA-Z0-9@]+/, '');
};
<input type="text" id="input-field" onkeyup="validate();"/>

如果您能以最简单的方式更改上面的 xsl 并添加 jquery/javascript 验证,我们将不胜感激

最佳答案

所以你可以这样做:

给定输入文档...

<t>
  <attribute>
    <controlType>TextBox</controlType>
    <name>some-name</name>
    <data>some-datum</data>
    <id>input-field</id>
    <width-on-form>180</width-on-form>
  </attribute>
</t>

...应用 XSLT 2.0 样式表...

<xsl:transform
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
<xsl:output method="html" version="5" encoding="UTF-8" />
<xsl:strip-space elements="*" />

<xsl:template match="/">
  <html>
    <head>
      <title>Some form</title>
    </head>
    <body>
      <xsl:apply-templates />
    </body>        
  </html>
</xsl:template>

<xsl:template match="t">
  <form>
   <xsl:apply-templates />
  </form>
</xsl:template>


<xsl:template match="attribute[controlType eq 'TextBox']">
  <input type="text" id="{id}" name="{name}" style="width:{width-on-form}pt" value="{data}" pattern="[a-zA-Z0-9@]+" />
</xsl:template>

</xsl:transform>

...将产生输出...

<!DOCTYPE HTML>
<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Some form</title>
   </head>
   <body>
     <form>
       <input
           type="text"
           id="input-field"
           name="some-name"
           style="width:180pt"
           value="some-datum"
           pattern="[a-zA-Z0-9@]+">
     </form>
   </body>
</html>

关于xsl 文件中输入字段的 Javascript 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33633701/

相关文章:

javascript - 仅当显示模态时才下载模态中的图片

javascript - 如何使变量的首字母始终大写?

xml - 需要 XSL 帮助

xml - 使用 xslt 2.0 生成包含目录中文件列表的文档

xml - 如果使用 xslt 的 xml 中不存在子节点,则删除父节点

javascript - 将外部 "data.json"文件导入 javascript 变量

javascript - 当单行上有多个匹配项时,如何仅找到 `' `(具有最小长度)之间的字符串?

javascript - 用于 XMLHttpRequest 或 ActiveXObject 的 JavaScript 中 jQuery 的 .ajaxComplete() 替代方案

javascript - 新的 DIV 出现在另一个 Div 的悬停/鼠标悬停上

jquery - 页面加载时打开第一个 child