Debug School

rakesh kumar
rakesh kumar

Posted on • Updated on

list out checklist of different kind of url validation using jquery

Basic URL Format Validation(starts with https/ftp)
Protocol Restriction using startswith (http or https)
startsWith http and includes social site keywords
Social url Length Limitations

Basic URL Format Validation:

Ensure the URL starts with a valid scheme and check for valid characters.
Example:

$('#urlInput').on('input', function() {
    const userInput = $(this).val();
    const urlRegex = /^(https?|ftp):\/\/[^\s/$.?#].[^\s]*$/;
    const isValidUrl = urlRegex.test(userInput);

    // Handle validation result (e.g., update UI, show/hide error messages)
});
Enter fullscreen mode Exit fullscreen mode

Protocol Restriction:

If your application only accepts specific protocols, restrict the input to those protocols.
Example:

$('#urlInput').on('input', function() {
    const userInput = $(this).val();
    const allowedProtocols = ['http', 'https'];
    const isValidProtocol = allowedProtocols.some(protocol => userInput.startsWith(protocol));

    // Handle validation result
});
Enter fullscreen mode Exit fullscreen mode

Domain Validation:

Check if the domain is valid (exists and has a valid DNS record).
Example:

$('#urlInput').on('input', function() {
    const userInput = $(this).val();
    const isValidDomain = /\.[a-z]{2,}$/.test(userInput) && userInput.indexOf(' ') === -1;

    // Handle validation result
});
Enter fullscreen mode Exit fullscreen mode

Path Validation:

Ensure that the path portion of the URL is well-formed.
Example:

$('#urlInput').on('input', function() {
    const userInput = $(this).val();
    try {
        const path = new URL(userInput).pathname;
        // Valid path
    } catch (error) {
        // Invalid path
    }
});
Enter fullscreen mode Exit fullscreen mode

Query Parameter Validation:

If the URL contains query parameters, validate them.
Example:

$('#urlInput').on('input', function() {
    const userInput = $(this).val();
    const isValidQuery = /^[a-zA-Z0-9=&]+$/.test(new URL(userInput).searchParams.toString());

    // Handle validation result
});
Enter fullscreen mode Exit fullscreen mode

Fragment Identifier Validation:

If the URL contains a fragment identifier, validate it.
Example:

$('#urlInput').on('input', function() {
    const userInput = $(this).val();
    const isValidFragment = !new URL(userInput).hash.startsWith("#");

    // Handle validation result
});
Enter fullscreen mode Exit fullscreen mode

Basic URL Format Validation:

Ensure the URL starts with a valid scheme (https://) and check for valid characters.
Example:

$('#socialUrlInput').on('input', function() {
    const userInput = $(this).val();
    const urlRegex = /^https:\/\/(www\.)?facebook\.com\/[a-zA-Z0-9.]+$/;
    const isValidFacebookUrl = urlRegex.test(userInput);

    // Handle validation result
});
Enter fullscreen mode Exit fullscreen mode

Domain Validation:

Check if the URL belongs to the expected social media domain.
Example:

$('#socialUrlInput').on('input', function() {
    const userInput = $(this).val();
    const validSocialDomains = ['facebook.com', 'twitter.com', 'instagram.com'];
    const isValidSocialUrl = validSocialDomains.some(domain => userInput.includes(domain));

    // Handle validation result
});
Enter fullscreen mode Exit fullscreen mode

Specific Social Media Validation:

Customize validation for specific social media platforms.
Example (for Facebook):

$('#socialUrlInput').on('input', function() {
    const userInput = $(this).val();
    const isValidFacebookUrl = userInput.startsWith('https://www.facebook.com/');

    // Handle validation result
});
Enter fullscreen mode Exit fullscreen mode

Profile/Page Validation:

Check if the URL points to a user profile or a page on the social media platform.
Example (for Facebook):

$('#socialUrlInput').on('input', function() {
    const userInput = $(this).val();
    const isValidFacebookProfile = /\/(profile|pages)\//.test(userInput);

    // Handle validation result
});
Enter fullscreen mode Exit fullscreen mode

Handle Vanity URLs:

Consider handling vanity URLs if applicable for the social media platform.
Example (for Facebook):

$('#socialUrlInput').on('input', function() {
    const userInput = $(this).val();
    const isValidFacebookVanityUrl = /\/[a-zA-Z0-9.]+\/?$/.test(userInput);

    // Handle validation result
});
Enter fullscreen mode Exit fullscreen mode

Length Limitations:

Set a maximum length for the social media URL to prevent abuse or errors.
Example:

$('#socialUrlInput').on('input', function() {
    const userInput = $(this).val();
    const maxUrlLength = 255;
    const isWithinMaxLength = userInput.length <= maxUrlLength;

    // Handle validation result
});
Enter fullscreen mode Exit fullscreen mode

These examples are tailored for Facebook, but you can adapt them for other social media platforms by adjusting the URL patterns and validation rules accordingly. Ensure that the rules align with the structure of the social media URLs you're expecting from users.

// Assuming 'url' is the variable containing the URL

if (url.includes('facebook') && (url.startsWith('http://') || url.startsWith('https://'))) {
  // The URL contains 'facebook' and starts with either 'http' or 'https'
  console.log('Valid Facebook URL');
} else {
  // Either 'facebook' is not present or the URL doesn't start with 'http' or 'https'
  console.log('Invalid Facebook URL');
}
Enter fullscreen mode Exit fullscreen mode

other way

  if (platform === 'facebook') {
        // Validation logic for Facebook URL
        return (url.includes('facebook') && (url.startsWith('http://') || url.startsWith('https://'))); 
    } else if (platform === 'twitter') {
        // Validation logic for Twitter URL
        // Replace the following line with your specific Twitter validation logic
        return (url.includes('twitter') && (url.startsWith('http://') || url.startsWith('https://')));
    } else if (platform === 'youtube') {
        // Validation logic for YouTube URL
        // Replace the following line with your specific YouTube validation logic
        return (url.includes('youtube') && (url.startsWith('http://') || url.startsWith('https://')));
    }
    else if (platform === 'wordpress') {
        // Validation logic for Twitter URL
        // Replace the following line with your specific Twitter validation logic
        return (url.includes('wordpress') && (url.startsWith('http://') || url.startsWith('https://')));
    } 
     else if (platform === 'tumblr') {
        // Validation logic for YouTube URL
        // Replace the following line with your specific YouTube validation logic
        return (url.includes('tumblr') && (url.startsWith('http://') || url.startsWith('https://')));
    }
    else if (platform === 'instagram') {
        // Validation logic for Twitter URL
        // Replace the following line with your specific Twitter validation logic
        return (url.includes('instagram') && (url.startsWith('http://') || url.startsWith('https://')));
    } else if (platform === 'quora') {
        // Validation logic for YouTube URL
        // Replace the following line with your specific YouTube validation logic
        return (url.includes('quora') && (url.startsWith('http://') || url.startsWith('https://')));
    }
    else if (platform === 'pinterest') {
        // Validation logic for Twitter URL
        // Replace the following line with your specific Twitter validation logic
        return (url.includes('pinterest') && (url.startsWith('http://') || url.startsWith('https://')));
    } else if (platform === 'reddit') {
        // Validation logic for YouTube URL
        // Replace the following line with your specific YouTube validation logic
        return (url.includes('reddit') && (url.startsWith('http://') || url.startsWith('https://')));
    }
    else if (platform === 'koo') {
        // Validation logic for Twitter URL
        // Replace the following line with your specific Twitter validation logic
        return (url.includes('koo') && (url.startsWith('http://') || url.startsWith('https://')));
    } else if (platform === 'scoopit') {
        // Validation logic for YouTube URL
        // Replace the following line with your specific YouTube validation logic
        return (url.includes('scoopit')&& (url.startsWith('http://') || url.startsWith('https://')));
    }
    else if (platform === 'slashdot') {
        // Validation logic for Twitter URL
        // Replace the following line with your specific Twitter validation logic
        return (url.includes('slashdot') && (url.startsWith('http://') || url.startsWith('https://')));
    } else if (platform === 'telegram') {
        // Validation logic for YouTube URL
        // Replace the following line with your specific YouTube validation logic
        return (url.includes('telegram') && (url.startsWith('http://') || url.startsWith('https://')));
    }
    else if (platform === 'fb_grp') {
        // Validation logic for YouTube URL
        // Replace the following line with your specific YouTube validation logic
        return (url.includes('fb_grp') && (url.startsWith('http://') || url.startsWith('https://')));
    }
    else if (platform === 'linkedin_grp') {

        return (url.includes('linkedin_grp') && (url.startsWith('http://') || url.startsWith('https://')));
    } else if (platform === 'linkedin') {

        return (url.includes('linkedin') && (url.startsWith('http://') || url.startsWith('https://')));
    }
    else if (platform === 'chingari') {

        return (url.includes('chingari') && (url.startsWith('http://') || url.startsWith('https://')));
    }
    else if (platform === 'roposo') {

        return (url.includes('roposo') && (url.startsWith('http://') || url.startsWith('https://')));
    } 
    else if (platform === 'mitron') {

      return (url.includes('mitron') && (url.startsWith('http://') || url.startsWith('https://')));
  } 

    else {
        // Default case for other platforms (return false for now)
        return false;
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)