ÿþ/ /   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 / /   J a v a s c r i p t   f o r m   v a l i d a t i o n   r o u t i n e s . 
 
 / /   A u t h o r :   S t e p h e n   P o l e y 
 
 / / 
 
 / /   S i m p l e   r o u t i n e s   t o   q u i c k l y   p i c k   u p   o b v i o u s   t y p o s . 
 
 / /   A l l   v a l i d a t i o n   r o u t i n e s   r e t u r n   t r u e   i f   e x e c u t e d   b y   a n   o l d e r   b r o w s e r : 
 
 / /   i n   t h i s   c a s e   v a l i d a t i o n   m u s t   b e   l e f t   t o   t h e   s e r v e r . 
 
 / / 
 
 / /   U p d a t e   A u g   2 0 0 4 :   h a v e   t e s t e d   t h a t   I E   5 . 0   a n d   I E   5 . 5   b o t h   s u p p o r t   D O M   m o d e l 
 
 / /   s u f f i c i e n t l y   w e l l ,   s o   i n n e r H T M L   o p t i o n   r e m o v e d   ( r e d u n d a n t ) . 
 
 / / 
 
 / /   U p d a t e   J u n   2 0 0 5 :   d i s c o v e r e d   t h a t   r e a s o n   I E   w a s n ' t   s e t t i n g   f o c u s   w a s 
 
 / /   d u e   t o   a n   I E   t i m i n g   b u g .   A d d e d   0 . 1   s e c   d e l a y   t o   f i x . 
 
 / / 
 
 / /   U p d a t e   O c t   2 0 0 5 :   m i n o r   t i d y - u p :   u n u s e d   p a r a m e t e r   r e m o v e d 
 
 / /   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 v a r   n b s p   =   1 6 0 ;         / /   n o n - b r e a k i n g   s p a c e   c h a r 
 
 v a r   n o d e _ t e x t   =   3 ;   / /   D O M   t e x t   n o d e - t y p e 
 
 v a r   e m p t y S t r i n g   =   / ^ \ s * $ / 
 
 v a r   g l b _ v f l d ;             / /   r e t a i n   v f l d   f o r   t i m e r   t h r e a d 
 
 
 
 / /   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 / /                                     t r i m 
 
 / /   T r i m   l e a d i n g / t r a i l i n g   w h i t e s p a c e   o f f   s t r i n g 
 
 / /   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 f u n c t i o n   t r i m ( s t r ) 
 
 { 
 
     r e t u r n   s t r . r e p l a c e ( / ^ \ s + | \ s + $ / g ,   ' ' ) 
 
 } ; 
 
 
 
 
 
 / /   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 / /                                     s e t f o c u s 
 
 / /   D e l a y e d   f o c u s   s e t t i n g   t o   g e t   a r o u n d   I E   b u g 
 
 / /   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 f u n c t i o n   s e t F o c u s D e l a y e d ( ) 
 
 { 
 
     g l b _ v f l d . f o c u s ( ) 
 
 } 
 
 
 
 f u n c t i o n   s e t f o c u s ( v f l d ) 
 
 { 
 
     / /   s a v e   v f l d   i n   g l o b a l   v a r i a b l e   s o   v a l u e   r e t a i n e d   w h e n   r o u t i n e   e x i t s 
 
     g l b _ v f l d   =   v f l d ; 
 
     s e t T i m e o u t (   ' s e t F o c u s D e l a y e d ( ) ' ,   1 0 0   ) ; 
 
 } 
 
 
 
 
 
 / /   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 / /                                     m s g 
 
 / /   D i s p l a y   w a r n / e r r o r   m e s s a g e   i n   H T M L   e l e m e n t 
 
 / /   c o m m o n C h e c k   r o u t i n e   m u s t   h a v e   p r e v i o u s l y   b e e n   c a l l e d 
 
 / /   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 f u n c t i o n   m s g ( f l d ,           / /   i d   o f   e l e m e n t   t o   d i s p l a y   m e s s a g e   i n 
 
                           m s g t y p e ,   / /   c l a s s   t o   g i v e   e l e m e n t   ( " w a r n "   o r   " e r r o r " ) 
 
                           m e s s a g e )   / /   s t r i n g   t o   d i s p l a y 
 
 { 
 
     / /   s e t t i n g   a n   e m p t y   s t r i n g   c a n   g i v e   p r o b l e m s   i f   l a t e r   s e t   t o   a   
 
     / /   n o n - e m p t y   s t r i n g ,   s o   e n s u r e   a   s p a c e   p r e s e n t .   ( F o r   M o z i l l a   a n d   O p e r a   o n e   c o u l d   
 
     / /   s i m p l y   u s e   a   s p a c e ,   b u t   I E   d e m a n d s   s o m e t h i n g   m o r e ,   l i k e   a   n o n - b r e a k i n g   s p a c e . ) 
 
     v a r   d i s p m e s s a g e ; 
 
     i f   ( e m p t y S t r i n g . t e s t ( m e s s a g e ) )   
 
         d i s p m e s s a g e   =   S t r i n g . f r o m C h a r C o d e ( n b s p ) ;         
 
     e l s e     
 
         d i s p m e s s a g e   =   m e s s a g e ; 
 
 
 
     v a r   e l e m   =   d o c u m e n t . g e t E l e m e n t B y I d ( f l d ) ; 
 
     e l e m . f i r s t C h i l d . n o d e V a l u e   =   d i s p m e s s a g e ;     
 
     
 
     e l e m . c l a s s N a m e   =   m s g t y p e ;       / /   s e t   t h e   C S S   c l a s s   t o   a d j u s t   a p p e a r a n c e   o f   m e s s a g e 
 
 } ; 
 
 
 
 / /   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 / /                         c o m m o n C h e c k 
 
 / /   C o m m o n   c o d e   f o r   a l l   v a l i d a t i o n   r o u t i n e s   t o : 
 
 / /   ( a )   c h e c k   f o r   o l d e r   /   l e s s - e q u i p p e d   b r o w s e r s 
 
 / /   ( b )   c h e c k   i f   e m p t y   f i e l d s   a r e   r e q u i r e d 
 
 / /   R e t u r n s   t r u e   ( v a l i d a t i o n   p a s s e d ) ,   
 
 / /                   f a l s e   ( v a l i d a t i o n   f a i l e d )   o r   
 
 / /                   p r o c e e d   ( d o n ' t   k n o w   y e t ) 
 
 / /   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 v a r   p r o c e e d   =   2 ;     
 
 
 
 f u n c t i o n   c o m m o n C h e c k         ( v f l d ,       / /   e l e m e n t   t o   b e   v a l i d a t e d 
 
                                                   i f l d ,       / /   i d   o f   e l e m e n t   t o   r e c e i v e   i n f o / e r r o r   m s g 
 
                                                   r e q d )       / /   t r u e   i f   r e q u i r e d 
 
 { 
 
     i f   ( ! d o c u m e n t . g e t E l e m e n t B y I d )   
 
         r e t u r n   t r u e ;     / /   n o t   a v a i l a b l e   o n   t h i s   b r o w s e r   -   l e a v e   v a l i d a t i o n   t o   t h e   s e r v e r 
 
     v a r   e l e m   =   d o c u m e n t . g e t E l e m e n t B y I d ( i f l d ) ; 
 
     i f   ( ! e l e m . f i r s t C h i l d ) 
 
         r e t u r n   t r u e ;     / /   n o t   a v a i l a b l e   o n   t h i s   b r o w s e r   
 
     i f   ( e l e m . f i r s t C h i l d . n o d e T y p e   ! =   n o d e _ t e x t ) 
 
         r e t u r n   t r u e ;     / /   i f l d   i s   w r o n g   t y p e   o f   n o d e     
 
 
 
     i f   ( e m p t y S t r i n g . t e s t ( v f l d . v a l u e ) )   { 
 
         i f   ( r e q d )   { 
 
             m s g   ( i f l d ,   " e r r o r " ,   " *   p o l e   w y m a g a n e " ) ;     
 
             s e t f o c u s ( v f l d ) ; 
 
             r e t u r n   f a l s e ; 
 
         } 
 
         e l s e   { 
 
             m s g   ( i f l d ,   " w a r n " ,   " " ) ;       / /   O K 
 
             r e t u r n   t r u e ;     
 
         } 
 
     } 
 
     r e t u r n   p r o c e e d ; 
 
 } 
 
 
 
 / /   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 / /                         v a l i d a t e P r e s e n t 
 
 / /   V a l i d a t e   i f   s o m e t h i n g   h a s   b e e n   e n t e r e d 
 
 / /   R e t u r n s   t r u e   i f   s o   
 
 / /   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 f u n c t i o n   v a l i d a t e P r e s e n t ( v f l d ,       / /   e l e m e n t   t o   b e   v a l i d a t e d 
 
                                                   i f l d   )     / /   i d   o f   e l e m e n t   t o   r e c e i v e   i n f o / e r r o r   m s g 
 
 { 
 
     v a r   s t a t   =   c o m m o n C h e c k   ( v f l d ,   i f l d ,   t r u e ) ; 
 
     i f   ( s t a t   ! =   p r o c e e d )   r e t u r n   s t a t ; 
 
 
 
     m s g   ( i f l d ,   " w a r n " ,   " " ) ;     
 
     r e t u r n   t r u e ; 
 
 } ; 
 
 
 
 / /   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 / /                               v a l i d a t e E m a i l 
 
 / /   V a l i d a t e   i f   e - m a i l   a d d r e s s 
 
 / /   R e t u r n s   t r u e   i f   s o   ( a n d   a l s o   i f   c o u l d   n o t   b e   e x e c u t e d   b e c a u s e   o f   o l d   b r o w s e r ) 
 
 / /   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 f u n c t i o n   v a l i d a t e E m a i l     ( v f l d ,       / /   e l e m e n t   t o   b e   v a l i d a t e d 
 
                                                   i f l d ,       / /   i d   o f   e l e m e n t   t o   r e c e i v e   i n f o / e r r o r   m s g 
 
                                                   r e q d )       / /   t r u e   i f   r e q u i r e d 
 
 { 
 
     v a r   s t a t   =   c o m m o n C h e c k   ( v f l d ,   i f l d ,   r e q d ) ; 
 
     i f   ( s t a t   ! =   p r o c e e d )   r e t u r n   s t a t ; 
 
 
 
     v a r   t f l d   =   t r i m ( v f l d . v a l u e ) ;     / /   v a l u e   o f   f i e l d   w i t h   w h i t e s p a c e   t r i m m e d   o f f 
 
     v a r   e m a i l   =   / ^ [ ^ @ ] + @ [ ^ @ . ] + \ . [ ^ @ ] * \ w \ w $ / 
 
     i f   ( ! e m a i l . t e s t ( t f l d ) )   { 
 
         m s g   ( i f l d ,   " e r r o r " ,   " B AD :   n i e p o p r a w n y   e m a i l " ) ; 
 
         s e t f o c u s ( v f l d ) ; 
 
         r e t u r n   f a l s e ; 
 
     } 
 
 
 
     v a r   e m a i l 2   =   / ^ [ A - Z a - z ] [ \ w . - ] + @ \ w [ \ w . - ] + \ . [ \ w . - ] * [ A - Z a - z ] [ A - Z a - z ] $ / 
 
     i f   ( ! e m a i l 2 . t e s t ( t f l d ) )   
 
         m s g   ( i f l d ,   " w a r n " ,   " U W A G A :   n i e s t a n d a r d o w y   e m a i l " ) ; 
 
     e l s e 
 
         m s g   ( i f l d ,   " w a r n " ,   " " ) ; 
 
     r e t u r n   t r u e ; 
 
 } ; 
 
 
 
 
 
 / /   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 / /                         v a l i d a t e T e l n r 
 
 / /   V a l i d a t e   t e l e p h o n e   n u m b e r 
 
 / /   R e t u r n s   t r u e   i f   s o   ( a n d   a l s o   i f   c o u l d   n o t   b e   e x e c u t e d   b e c a u s e   o f   o l d   b r o w s e r ) 
 
 / /   P e r m i t s   s p a c e s ,   h y p h e n s ,   b r a c k e t s   a n d   l e a d i n g   + 
 
 / /   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
 
 
 
 f u n c t i o n   v a l i d a t e T e l n r     ( v f l d ,       / /   e l e m e n t   t o   b e   v a l i d a t e d 
 
                                                   i f l d ,       / /   i d   o f   e l e m e n t   t o   r e c e i v e   i n f o / e r r o r   m s g 
 
                                                   r e q d )       / /   t r u e   i f   r e q u i r e d 
 
 { 
 
     v a r   s t a t   =   c o m m o n C h e c k   ( v f l d ,   i f l d ,   r e q d ) ; 
 
     i f   ( s t a t   ! =   p r o c e e d )   r e t u r n   s t a t ; 
 
 
 
     v a r   t f l d   =   t r i m ( v f l d . v a l u e ) ;     / /   v a l u e   o f   f i e l d   w i t h   w h i t e s p a c e   t r i m m e d   o f f 
 
     v a r   t e l n r   =   / ^ \ + ? [ 0 - 9   ( ) - ] + [ 0 - 9 ] $ / 
 
     i f   ( ! t e l n r . t e s t ( t f l d ) )   { 
 
         m s g   ( i f l d ,   " e r r o r " ,   " B AD :   n i e p o p r a w n y   n u m e r   t e l e f o n u .   D o z w o l o n e   s   c y f r y ,   p r z e r w y   i   p o p r z e d z a j c y   + " ) ; 
 
         s e t f o c u s ( v f l d ) ; 
 
         r e t u r n   f a l s e ; 
 
     } 
 
 
 
     v a r   n u m d i g i t s   =   0 ; 
 
     f o r   ( v a r   j = 0 ;   j < t f l d . l e n g t h ;   j + + ) 
 
         i f   ( t f l d . c h a r A t ( j ) > = ' 0 '   & &   t f l d . c h a r A t ( j ) < = ' 9 ' )   n u m d i g i t s + + ; 
 
 
 
     i f   ( n u m d i g i t s < 6 )   { 
 
         m s g   ( i f l d ,   " e r r o r " ,   " B AD :   T y l k o   "   +   n u m d i g i t s   +   "   c y f r y   t o   n u m e r   z a   k r ó t k i " ) ; 
 
         s e t f o c u s ( v f l d ) ; 
 
         r e t u r n   f a l s e ; 
 
     } 
 
 
 
     i f   ( n u m d i g i t s > 1 4 ) 
 
         m s g   ( i f l d ,   " w a r n " ,   " U W A G A :   "   +   n u m d i g i t s   +   "   c y f r   t o   n u m e r   d o [  d Bu g i " ) ; 
 
     e l s e   {   
 
         i f   ( n u m d i g i t s < 1 0 ) 
 
             m s g   ( i f l d ,   " w a r n " ,   " U W A G A :   t y l k o   "   +   n u m d i g i t s   +   "   c y f r   -   s p r a w d z  n u m e r " ) ; 
 
         e l s e 
 
             m s g   ( i f l d ,   " w a r n " ,   " " ) ; 
 
     } 
 
     r e t u r n   t r u e ; 
 
 } ; 
