数据验证一般包括三种类型:
- 完整性(Completeness):用户必须填充所有要求的字段。有时候,某个字段是否必须填充依赖于另一个字段的值。比如,只有当用户使用信用卡支付时才需要信用卡号。
- 数据类型(Data types): 数字必须是数字、日期必须是日期,依次类推。
- 合理性(Appropriateness):电话号码应该只包含数字,或者还可以包含与区号有关的短线和括号。邮政编码必须有 5 个数字组成,还可以带有短线和“加四”号码。
XForm提供Model中数据节点的类型绑定机制,如:
<xforms:model id=“payinfo“>
<xforms:submission action="http://www.example.com/orderform.php"
method="post"/>
<xforms:instance xmlns="">
<paymentinfo>
<method></method>
<cardtype></cardtype>
<cardnumber></cardnumber>
<expdate></expdate>
</paymentinfo>
</xforms:instance>
<xforms:bind ref="paymentinfo" type="ccnumber"/>
<xsd:schema>
<xsd:simpleType name="ccnumber">
<xsd:restriction base="xsd:string">
<xsd:pattern value="\d{14,18}"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:schema>
</xforms:model>