Thread: [Xsltforms-support] Validation with regex?
Brought to you by:
alain-couthures
From: Elias M. <eli...@gm...> - 2013-05-09 17:57:32
|
Hi. I'm trying to validate a field that holds a file name to be stored in exist-db. I want to ensure only characters, numbers, space and . , _ - characters are allowed. I know I can use not(contains()) like this: <xf:bind nodeset="instance('local')/FileName" type="xsd:string" required="true()" constraint="not(contains(.,'!')) and not(contains(.,'$'))"/> but that means doing for every character not allowed. Is there a better way, like a regexp match? Thanks. Elias |
From: Alain C. <ala...@ag...> - 2013-05-09 18:58:54
|
Hi Elias, > I'm trying to validate a field that holds a file name to be stored in > exist-db. I want to ensure only characters, numbers, space and . , _ > - characters are allowed. I know I can use not(contains()) like this: > > <xf:bind nodeset="instance('local')/FileName" type="xsd:string" > required="true()" > constraint="not(contains(.,'!')) and not(contains(.,'$'))"/> > > but that means doing for every character not allowed. Is there a > better way, like a regexp match? XSLTForms is not yet supporting XPath 2.0 and the matches() is not implemented (I can add it quite easily actually...). But, as a workaround, you can define a schema type for this. In the Test Suite, there is an example for a credit card number: <html xmlns:xforms="http://www.w3.org/2002/xforms"xmlns:my="http://commerce.example.com/payment"xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:txs="http://www.agencexml.com/txs"xmlns="http://www.w3.org/1999/xhtml"xmlns:xhtml="http://www.w3.org/1999/xhtml"> <head> <title>2.3.a Example: Value Constraints</title> <link rel="stylesheet"href="../driverPages/forms/TestSuite11.css <view-source:http://localhost/xsltforms/trunk/src/testsuite/w3c/1.1/tests/Edition1/driverPages/forms/TestSuite11.css>"type="text/css"/> <xforms:model> <xforms:instance> <my:payment xmlns="http://commerce.example.com/payment"method="cc"> <my:number/> <my:expiry/> </my:payment> </xforms:instance> <xforms:submission id="submit01"method="post"action="../../../../../../echo.php <view-source:http://localhost/xsltforms/trunk/src/echo.php>"/> <xforms:bind nodeset="/my:payment/my:number"relevant="/my:payment/@method = 'cc'"required="/my:payment/@method='cc'"type="my:ccnumber"/> <xforms:bind nodeset="/my:payment/my:expiry"relevant="/my:payment/@method = 'cc'"required="/my:payment/@method='cc'"type="xsd:gYearMonth"/> <xsd:schema targetNamespace="http://commerce.example.com/payment"> <xsd:simpleType name="ccnumber"> <xsd:restriction base="xsd:string"> <xsd:pattern value="\d{14,18}"/> </xsd:restriction> </xsd:simpleType> <xsd:element name="payment"> <xsd:complexType> <xsd:sequence> <xsd:element name="number"type="my:ccnumber"minOccurs="0"/> <xsd:element name="expiry"type="xsd:gYearMonth"minOccurs="0"/> </xsd:sequence> <xsd:attribute name="method"type="xsd:string"/> </xsd:complexType> </xsd:element> </xsd:schema> </xforms:model> </head> <body> <xforms:group> <xforms:label class="title">2.3.a Example: Value Constraints</xforms:label> </xforms:group> <xforms:group> <xforms:label> You must see a select1 control with the values "Cash" and "Credit" as well as two input controls on the page. The Credit Card Number and Expiration Date input controls are set to be relevant only when Credit is selected. If you have selected Cash then you must be able to submit the form and the input controls must have become unavailable. </xforms:label> </xforms:group> <xforms:group> <xforms:label> If you have selected Credit then you must not be able to submit the form until valid data is entered into both input controls. A valid entry for Credit Card Number is a positive number with 14-18 digits. A valid entry for the expiration date is a date in the format of gYearMonth, which is yyyy-mm (ex. 1998-12 for December 1998). </xforms:label> </xforms:group> <xforms:group> <xforms:label> When you activate the Submit Now submit control this page must be replaced by the form data. You must see the value "cc" and the values you entered in the Credit Card Number and Expiration Date input controls if you had selected Credit or the value "cash" if you had selected Cash. </xforms:label> </xforms:group> <xforms:select1 ref="@method"> <xforms:label>Select Payment Method:</xforms:label> <xforms:item> <xforms:label>Cash</xforms:label> <xforms:value>cash</xforms:value> </xforms:item> <xforms:item> <xforms:label>Credit</xforms:label> <xforms:value>cc</xforms:value> </xforms:item> </xforms:select1> <xforms:input ref="my:number"> <xforms:label>Credit Card Number:</xforms:label> </xforms:input> <xforms:input ref="/my:payment/my:expiry"> <xforms:label>Expiration Date:</xforms:label> </xforms:input> <xforms:submit submission="submit01"> <xforms:label>Submit Now</xforms:label> </xforms:submit> </body> </html> Thank you for your feedbacks! -Alain |