Jump to content

XForms/Case Validation

From Wikibooks, open books for an open world

Motivation

[edit | edit source]

You want to check to see if a field is upper or lower case.

Note: This does not seem to work in FireFox or XSLTForms.

Method

[edit | edit source]

We will use a bind constraint.


  <xf:bind nodeset="UpperCaseText" type="xs:string"  
           constraint="
                 . eq translate(., 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') 
                     "/>

Sample Source Code

[edit | edit source]
<html
    xmlns="http://www.w3.org/1999/xhtml"
    xmlns:xf="http://www.w3.org/2002/xforms" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    >
    <head>
        <title>Case Test</title>
        <style type="text/css">
        <![CDATA[
            body {
            font-family: Helvetica, sans-serif;
            }
        ]]>
        </style>
        <xf:model>
            <xf:instance xmlns="">
               <data>
                    <LowerCaseText>abc</LowerCaseText>
                    <UpperCaseText>ABC</UpperCaseText>
                </data>
            </xf:instance>
            
            
            <xf:bind nodeset="UpperCaseText" type="xs:string"  
            constraint="
                  . eq translate(., 'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ') "/>
           
            <!-- compare the string to the lower-case version.  If they are the same we pass -->
            <xf:bind nodeset="LowerCaseText" type="xs:string" 
            constraint="
                    . eq translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') "/>
   
        </xf:model>
    </head>
    <body>
        <h1>Case Test</h1>
        <p>To pass this test the form must warn the user if they enter an invalid string. 
        The XForms alert message
        must also be visible.</p>
        
            <xf:input ref="LowerCaseText" incremental="true">
                <xf:label>Lower Case String:</xf:label>
                <xf:alert>The string must contain lowercase only.</xf:alert>
            </xf:input>
            <br/>
            
            <xf:input  ref="UpperCaseText" incremental="true">
                <xf:label>Upper Case String:</xf:label>
                <xf:alert>The string must contain uppercase only.</xf:alert>
            </xf:input>
            <br/>
    </body>
</html>


Next Page: Submit | Previous Page: Validation with Bind
Home: XForms