Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    68
    Thanks
    29
    Thanked 0 Times in 0 Posts

    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>

  • #2
    New Coder
    Join Date
    Oct 2010
    Posts
    68
    Thanks
    29
    Thanked 0 Times in 0 Posts
    never mind I forgot I can just use variables stored in another file


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •