Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-14-2012, 10:17 PM   PM User | #1
aaron4osu
New Coder

 
Join Date: Oct 2010
Posts: 66
Thanks: 29
Thanked 0 Times in 0 Posts
aaron4osu is an unknown quantity at this point
How to combine boilerplate javascript into other javascript

I have a bunch of different forms that have common fields. I have a long validation script where i want to extract the common fields so if I make a change to the message I don't have to repeat it. Is there something simmilar to php's include where I can place all of the js that overlaps and just include it into the script?

Here is the validation script:
Code:
<script type="text/javascript">
    $(document).ready(function(){

        $('#contact-form').validate({
          rules: {

            'audience[]': {required: true},
            
            task_name: {
              minlength: 2,
              required: true
            },

            request_desc: {
              minlength: 2,
              required: true
            },

            goal: {
              minlength: 2,
              required: true
            },

            support_location: {
              minlength: 2,
              required: true
            },

            requester_name: {
              minlength: 2,
              required: true
            },

            requester_phone: {
              minlength: 7,
              required: true
            },
            
            requester_email: {
              required: true,
              email: true
            },
            
            requester_depart: {
              required: true
            },

            approval_name: {
              minlength: 2,
              required: true
            },
            
            approval_email: {
              required: true,
              email: true
            },

            approval_name_2: {
              minlength: 2,
            },
            
            approval_email_3: {
              email: true
            },

            approval_name_2: {
              minlength: 2,
            },
            
            approval_email_3: {
              email: true
            },

            // end of base validation
          
            deadline_date:{
              
              required: true
            },
            
            event_name: {
              minlength: 2,
              required: true
            },

            event_url: {
              minlength: 2,
              required: true
            },

            franklin_sponsor: {
              minlength: 2,
              required: true
            },

            items: {
              minlength: 2,
              required: true
            },

            paid_by: {
              minlength: 2,
              required: true
            },  
          },
          messages: { 
                task_name: "Enter a task name",

                request_desc: "Enter a task description",

                goal: "Enter the goal of task",

                support_location: "Enter locations supporting request",

                requester_name: "Enter a Task Name",

                approval_name: "Enter a Task Name",

              requester_email: { 
                    required: "Please enter a valid email address", 
                    email: "Please enter a valid email address" 
                     
                },

                requester_phone: { 
                    required: "Please enter a valid phone number", 
                    minlength: "Must be at least 7 digits"
                    
                },

                approval_email: { 
                    required: "Please enter a valid email address", 
                    minlength: "Please enter a valid email address"
                    
                },

                approval_email_2: { 
                     
                    email: "Please enter a valid email address"
                },  

                approval_email_3: { 
                    
                    email: "Please enter a valid email address"
                },
                // end of basic messages  

                 deadline_date:{
              
                    required: "Please enter a deadline date ex. 01-11-2013"
                  },
                  
                  event_name: {
                    
                    required: "Please enter an event name"
                  },

                  event_url: {
                    
                    required: "Please enter an Event or organization URL"
                  },

                  franklin_sponsor: {
                    
                    required: "Enter an initiative or program"
                  },

                  items: {
                   
                    required: 'What items are included in this sponsorship package?'
                  },

                  paid_by: {
                    
                    required: 'Which department will be paying for this?'
                  },

                   'audience[]': "You must check at least 1 box",
              
            },

          errorPlacement: function( label, element ) {
            
            if( element.attr( "name" ) === "audience[]" || element.attr( "name" ) === "event_services[]" ) {
              element.parent().append( label ); // this would append the label after all your checkboxes/labels (so the error-label will be the last element in <div class="controls"> )
            } else {
              label.insertAfter( element ); // standard behaviour
            }
          },

          highlight: function(label) {
            $(label).closest('.control-group').addClass('error');
          },
          success: function(label) {
            label
              .text('OK!').addClass('valid')
              .closest('.control-group').addClass('success');
          }
           
        });
        
    }); // end document.ready
</script>
aaron4osu is offline   Reply With Quote
Old 11-15-2012, 03:41 PM   PM User | #2
aaron4osu
New Coder

 
Join Date: Oct 2010
Posts: 66
Thanks: 29
Thanked 0 Times in 0 Posts
aaron4osu is an unknown quantity at this point
never mind I forgot I can just use variables stored in another file
aaron4osu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 03:27 AM.


Advertisement
Log in to turn off these ads.