鼠标离开input框进行校验,点击则显示之前填过的数据。
在input中添加backValue属性用于记录value属性中的值。
stack用于记录是否还有错误存在。
以下代码来自xslt文件中的代码片段
<script language="JavaScript" type="text/javascript">
var stack=0;
function updateContract(){
if(stack=="0"){
document.getElementById("frm").action="updateContract.action";
document.getElementById("frm").submit();
}
}
function submitContract(){
document.getElementById("frm").action="submitContract.action";
document.getElementById("frm").submit();
}
function validData(inputName){
var inputObj=document.getElementsByName(inputName);
if(inputObj[0].ismust=="1"){
if(inputObj[0].value!=""){
inputObj[0].backValue=inputObj[0].value;
}else{
inputObj[0].value="必填项";
inputObj[0].backStyle=inputObj[0].style.cssText;
inputObj[0].style.cssText=inputObj[0].style.cssText+";BACKGROUND-COLOR:RED;";
stack++;
return;
}
}
if(inputObj[0].value!=""){
if(inputObj[0].itemType=="2"){
if(!(inputObj[0].value+'').match(/^\d+\.{0,1}\d+$/)){
inputObj[0].backValue=inputObj[0].value;
inputObj[0].value="只能填入数字";
inputObj[0].backStyle=inputObj[0].style.cssText;
inputObj[0].style.cssText=inputObj[0].style.cssText+";BACKGROUND-COLOR:RED;";
stack++;
return;
}
if(!!(inputObj[0].value+'').match(/^\d+\.{0,1}\d+$/)){
if(inputObj[0].value.length>inputObj[0].length){
inputObj[0].backValue=inputObj[0].value;
inputObj[0].value="只能输入"+inputObj[0].length+"个数字";
inputObj[0].backStyle=inputObj[0].style.cssText;
inputObj[0].style.cssText=inputObj[0].style.cssText+";BACKGROUND-COLOR:RED;";
stack++;
return;
}
}
}
}
stack--;
}
function resetData(inputName){
var inputObj=document.getElementsByName(inputName);
inputObj[0].style.cssText=inputObj[0].backStyle;
inputObj[0].value=inputObj[0].backValue;
}
</script>
<xsl:template match="INPUT">
<xsl:element name="input">
<xsl:attribute name="type">text</xsl:attribute>
<xsl:attribute name="name"><xsl:value-of select="./@name"/></xsl:attribute>
<xsl:attribute name="length"><xsl:value-of select="./@size"/></xsl:attribute>
<xsl:attribute name="ismust"><xsl:value-of select="./@ismust"/></xsl:attribute>
<xsl:attribute name="itemType"><xsl:value-of select="./@itemType"/></xsl:attribute>
<xsl:attribute name="id">inputId</xsl:attribute>
<xsl:attribute name="backValue"><xsl:value-of select="."/></xsl:attribute>
<xsl:attribute name="style">border:1px; border-bottom-style: solid;border-top-style: none;border-left-style:none;border-right-style:none;</xsl:attribute>
<xsl:attribute name="backStyle">border:1px; border-bottom-style: solid;border-top-style: none;border-left-style:none;border-right-style:none;</xsl:attribute>
<xsl:attribute name="onblur">validData('<xsl:value-of select="./@name"/>')</xsl:attribute>
<xsl:attribute name="onfocus">resetData('<xsl:value-of select="./@name"/>')</xsl:attribute>
<xsl:attribute name="value"><xsl:value-of select="."/></xsl:attribute>
</xsl:element>
</xsl:template>