Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5947951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:02:17+00:00 2026-05-22T17:02:17+00:00

I am doing a simple URL Builder in a backend of a website where

  • 0

I am doing a simple URL Builder in a backend of a website where I need a url like http://www.google.es/ where http:// and / are needed but the users usually inputs http://www.google.es or maxmium http://www.google.es.

I have this basic code where I check for not blank values and for a valid url:

$("#ga-generate").click(function(){
    var url = $("#ga-url").val();
    var nombre = $("#ga-nombre").val();

    if(url == "") {
        alert("URL is required");

        return false;
    }
    else {
        //set the new URL with http and / if needed

        $("#ga-url").val(url);
    }

    if(nombre == "") {
        alert("A name is required");

        return false;
    }

    if(/^([a-z]([a-z]|\d|\+|-|\.)*):(\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?((\[(|(v[\da-f]{1,}\.(([a-z]|\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F900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test(url)) {
        var final_url = "";

        final_url = url;    
    }
    else {
        alert("URL is not valid");
    }

});

The regex checks for a valid URL, it checks if it have http:// (and not a final / ) so I would like to add the http:// and / if it’s not added just before call the regex. How I can do it?

Thank you in advance!!

EDIT: for the first part, add http, there is a PHP function that I don’t know how to translate to javascript:

function addhttp($url) {
    if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
        $url = "http://" . $url;
    }
    return $url;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:02:18+00:00Added an answer on May 22, 2026 at 5:02 pm

    Did you want this is backend or frontend? You state you need it in backend, but question is only tagged with jquery.
    Also, you should be doing this check is backend regardless, since users can possibly circumvent javascript

    if(url.substr(0,7) != 'http://'){
        url = 'http://' + url;
    }
    if(url.substr(url.length-1, 1) != '/'){
        url = url + '/';
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a very simple YQL statement: select * from xml where url=http://www.wowhead.com/item=50278&xml The
I need to do a few very simple URL manipulations in Java. Like get
I am doing a simple $.ajax request: $.ajax({ type: POST, url: /Run/ + param1
I'm doing simple string input parsing and I am in need of a string
I have a very simple table on my website, that displays different URL's. I
I'm doing some simple HTTP authentication and am getting a java.lang.IllegalArgumentException: Illegal character(s) in
I'm doing a very simple Comet-like long polling in JavaScript The code below seems
I have a simple ajax call: $.ajax({ url: 'http://localhost:39657/List/Receptacle', dataType: json, success: function(json) {
I am writing a simple HttpHandler for URL rewriting, but I'm hitting a brick
I'm doing some url rewriting in PHP and need to find URLS with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.