2015-11-10 125 views
0

XSL文件我有以下的xsl:JavaScript验证在输入字段

<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(/[^[email protected]]+/, ''); 
}; 
<input type="text" id="input-field" onkeyup="validate();"/> 

中以最简单的方式改变了xsl以上,增加的jQuery/JavaScript验证任何帮助表示赞赏

+0

尝试''并在JS正则表达式中添加锚'/^[a-z _] $/i.test(keyChar)' – Tushar

+0

@Tushar调查此问题。请注意,它需要为IE8和一些老的浏览器,如Mozilla 4 – topcat3

回答

1

所以你可以做这样的事情:

给定的输入文件......

<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="[[email protected]]+" /> 
</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="[[email protected]]+"> 
 
    </form> 
 
    </body> 
 
</html>

+0

工作,这看起来非常好。感谢您的帮助,但我查了模式属性,它不适用于IE8 – topcat3

+0

@ topcat3:你可以使用JavaScript框架? –

+1

如果是这样,签出:jQuery工具验证器(http://jquerytools.github.io/documentation/validator/index.html)。您只需链接库并添加'$(“#myform”)。validator();'其中myform是表单标识。 –