| 
									
										
										
										
											2010-03-30 15:35:36 -04:00
										 |  |  | ! Copyright (C) 2006, 2010 Slava Pestov | 
					
						
							| 
									
										
										
										
											2008-05-22 23:41:23 -04:00
										 |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2011-10-14 15:31:06 -04:00
										 |  |  | USING: arrays assocs classes continuations hashtables kernel | 
					
						
							|  |  |  | make math math.functions math.parser math.ranges namespaces | 
					
						
							|  |  |  | quotations regexp sequences sets unicode.case unicode.categories | 
					
						
							|  |  |  | words xmode.catalog ;
 | 
					
						
							| 
									
										
										
										
											2008-05-22 23:41:23 -04:00
										 |  |  | IN: validators | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-11 00:53:23 -05:00
										 |  |  | : v-checkbox ( str -- ? )
 | 
					
						
							| 
									
										
										
										
											2009-01-13 22:58:39 -05:00
										 |  |  |     >lower "on" = ;
 | 
					
						
							| 
									
										
										
										
											2009-01-11 00:53:23 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-24 01:45:39 -04:00
										 |  |  | : v-default ( str def -- str/def )
 | 
					
						
							| 
									
										
										
										
											2011-10-14 15:31:06 -04:00
										 |  |  |     [ drop empty? not ] most ;
 | 
					
						
							| 
									
										
										
										
											2008-05-22 23:41:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : v-required ( str -- str )
 | 
					
						
							|  |  |  |     dup empty? [ "required" throw ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-24 01:45:39 -04:00
										 |  |  | : v-optional ( str quot -- result )
 | 
					
						
							| 
									
										
										
										
											2008-05-22 23:41:23 -04:00
										 |  |  |     over empty? [ 2drop f ] [ call ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : v-min-length ( str n -- str )
 | 
					
						
							|  |  |  |     over length over < [ | 
					
						
							|  |  |  |         [ "must be at least " % # " characters" % ] "" make | 
					
						
							|  |  |  |         throw
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         drop
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : v-max-length ( str n -- str )
 | 
					
						
							|  |  |  |     over length over > [ | 
					
						
							|  |  |  |         [ "must be no more than " % # " characters" % ] "" make | 
					
						
							|  |  |  |         throw
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         drop
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : v-number ( str -- n )
 | 
					
						
							|  |  |  |     dup string>number [ ] [ "must be a number" throw ] ?if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-26 01:48:02 -04:00
										 |  |  | : v-integer ( str -- n )
 | 
					
						
							|  |  |  |     v-number dup integer? [ "must be an integer" throw ] unless ;
 | 
					
						
							| 
									
										
										
										
											2008-05-22 23:41:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : v-min-value ( x n -- x )
 | 
					
						
							|  |  |  |     2dup < [ | 
					
						
							|  |  |  |         [ "must be at least " % # ] "" make throw
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         drop
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : v-max-value ( x n -- x )
 | 
					
						
							|  |  |  |     2dup > [ | 
					
						
							|  |  |  |         [ "must be no more than " % # ] "" make throw
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         drop
 | 
					
						
							|  |  |  |     ] if ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : v-regexp ( str what regexp -- str )
 | 
					
						
							| 
									
										
										
										
											2009-02-02 14:43:54 -05:00
										 |  |  |     3dup nip matches? | 
					
						
							|  |  |  |     [ 2drop ] [ drop "invalid " prepend throw ] if ;
 | 
					
						
							| 
									
										
										
										
											2008-05-22 23:41:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : v-email ( str -- str )
 | 
					
						
							|  |  |  |     #! From http://www.regular-expressions.info/email.html | 
					
						
							| 
									
										
										
										
											2009-02-02 14:43:54 -05:00
										 |  |  |     320 v-max-length | 
					
						
							| 
									
										
										
										
											2008-05-22 23:41:23 -04:00
										 |  |  |     "e-mail" | 
					
						
							|  |  |  |     R' [A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}'i | 
					
						
							|  |  |  |     v-regexp ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : v-url ( str -- str )
 | 
					
						
							| 
									
										
										
										
											2009-01-20 19:44:38 -05:00
										 |  |  |     "URL" R' (?:ftp|http|https)://\S+' v-regexp ;
 | 
					
						
							| 
									
										
										
										
											2008-05-22 23:41:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : v-captcha ( str -- str )
 | 
					
						
							|  |  |  |     dup empty? [ "must remain blank" throw ] unless ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : v-one-line ( str -- str )
 | 
					
						
							| 
									
										
										
										
											2008-05-26 01:48:02 -04:00
										 |  |  |     v-required | 
					
						
							| 
									
										
										
										
											2009-01-12 02:51:38 -05:00
										 |  |  |     dup "\r\n" intersects? | 
					
						
							|  |  |  |     [ "must be a single line" throw ] when ;
 | 
					
						
							| 
									
										
										
										
											2008-05-22 23:41:23 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : v-one-word ( str -- str )
 | 
					
						
							| 
									
										
										
										
											2008-05-26 01:48:02 -04:00
										 |  |  |     v-required | 
					
						
							| 
									
										
										
										
											2008-05-22 23:41:23 -04:00
										 |  |  |     dup [ alpha? ] all?
 | 
					
						
							|  |  |  |     [ "must be a single word" throw ] unless ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-05-26 01:48:02 -04:00
										 |  |  | : v-username ( str -- str )
 | 
					
						
							|  |  |  |     2 v-min-length 16 v-max-length v-one-word ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : v-password ( str -- str )
 | 
					
						
							|  |  |  |     6 v-min-length 40 v-max-length v-one-line ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : v-mode ( str -- str )
 | 
					
						
							|  |  |  |     dup mode-names member? [ | 
					
						
							|  |  |  |         "not a valid syntax mode" throw  | 
					
						
							|  |  |  |     ] unless ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-09-24 01:45:39 -04:00
										 |  |  | : luhn? ( str -- ? )
 | 
					
						
							| 
									
										
										
										
											2008-05-27 02:18:38 -04:00
										 |  |  |     string>digits <reversed>
 | 
					
						
							|  |  |  |     [ odd? [ 2 * 10 /mod + ] when ] map-index
 | 
					
						
							| 
									
										
										
										
											2011-10-14 15:31:06 -04:00
										 |  |  |     sum 10 divisor? ;
 | 
					
						
							| 
									
										
										
										
											2008-05-27 02:18:38 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | : v-credit-card ( str -- n )
 | 
					
						
							| 
									
										
										
										
											2010-03-16 20:17:26 -04:00
										 |  |  |     "- " without | 
					
						
							| 
									
										
										
										
											2008-05-27 02:18:38 -04:00
										 |  |  |     dup CHAR: 0 CHAR: 9 [a,b] diff empty? [ | 
					
						
							|  |  |  |         13 v-min-length | 
					
						
							|  |  |  |         16 v-max-length | 
					
						
							|  |  |  |         dup luhn? [ string>number ] [ | 
					
						
							|  |  |  |             "card number check failed" throw
 | 
					
						
							|  |  |  |         ] if
 | 
					
						
							|  |  |  |     ] [ | 
					
						
							|  |  |  |         "invalid credit card number format" throw
 | 
					
						
							|  |  |  |     ] if ;
 |