1 ! Copyright (C) 2006, 2008 Slava Pestov
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel continuations sequences math namespaces make sets
4 math.parser math.ranges assocs regexp unicode.categories arrays
5 hashtables words classes quotations xmode.catalog unicode.case ;
8 : v-checkbox ( str -- ? )
11 : v-default ( str def -- str/def )
14 : v-required ( str -- str )
15 dup empty? [ "required" throw ] when ;
17 : v-optional ( str quot -- result )
18 over empty? [ 2drop f ] [ call ] if ; inline
20 : v-min-length ( str n -- str )
22 [ "must be at least " % # " characters" % ] "" make
28 : v-max-length ( str n -- str )
30 [ "must be no more than " % # " characters" % ] "" make
36 : v-number ( str -- n )
37 dup string>number [ ] [ "must be a number" throw ] ?if ;
39 : v-integer ( str -- n )
40 v-number dup integer? [ "must be an integer" throw ] unless ;
42 : v-min-value ( x n -- x )
44 [ "must be at least " % # ] "" make throw
49 : v-max-value ( x n -- x )
51 [ "must be no more than " % # ] "" make throw
56 : v-regexp ( str what regexp -- str )
58 [ drop ] [ "invalid " prepend throw ] if ;
60 : v-email ( str -- str )
61 #! From http://www.regular-expressions.info/email.html
64 R' [A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}'i
67 : v-url ( str -- str )
68 "URL" R' (?:ftp|http|https)://\S+' v-regexp ;
70 : v-captcha ( str -- str )
71 dup empty? [ "must remain blank" throw ] unless ;
73 : v-one-line ( str -- str )
75 dup "\r\n" intersects?
76 [ "must be a single line" throw ] when ;
78 : v-one-word ( str -- str )
81 [ "must be a single word" throw ] unless ;
83 : v-username ( str -- str )
84 2 v-min-length 16 v-max-length v-one-word ;
86 : v-password ( str -- str )
87 6 v-min-length 40 v-max-length v-one-line ;
89 : v-mode ( str -- str )
90 dup mode-names member? [
91 "not a valid syntax mode" throw
95 string>digits <reversed>
96 [ odd? [ 2 * 10 /mod + ] when ] map-index
99 : v-credit-card ( str -- n )
101 dup CHAR: 0 CHAR: 9 [a,b] diff empty? [
104 dup luhn? [ string>number ] [
105 "card number check failed" throw
108 "invalid credit card number format" throw