SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    is there something similar in javascript to php's include?

    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. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,526
    Mentioned
    83 Post(s)
    Tagged
    3 Thread(s)
    Quote Originally Posted by aaron4osu View Post
    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?
    There is - they are called variables. For example:

    Code javascript:
    var emailMessage = 'Please enter a valid email address';
     
    ...
    requester_email: { 
        required: emailMessage,
        email: emailMessage
    },

    Or if you want to group all of the messages together:

    Code javascript:
    var messages = {
        task: 'Enter a task name',
        email: 'Please enter a valid email address',
        ...
    };
     
    ...
    requester_email: { 
        required: message.email,
        email: message.email
    },
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 2011
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lol how in the world did I not think of that. I must have been at the end of a caffeine crash. Thanks Paul.


Bookmarks

Posting Permissions

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