2

In my laravel app I process an external URL provided by user. Sometimes user would paste it without the http:// prefix. UI have used this question's answers: How to check URL contains http using JQuery and RegEx

I tried to use these instructions

var lnk = $('#confirmation_URL').val();
var lnk2 = $('#confirmation_URL').val();

//adding http if not present
// if (lnk && !lnk.match(/^.+:\/\/.*/)) {  // produces jQuery error
if (lnk && !lnk.match(/^http([s]?):\/\/.*/)) { // produces jQuery error

   var lnk = $('http://' + lnk2);
   console.log( "prefix http added successfully" );
}

When pasting an URL, I get this error:

jquery-2.1.4.min.js:2 Uncaught Error: Syntax error, unrecognized expression: http://laravel.com/docs/5.1/migrationsga.error @ jquery-2.1.4.min.js:2ga.tokenize @ jquery-2.1.4.min.js:2ga.select @ jquery-2.1.4.min.js:2ga @ jquery-2.1.4.min.js:2n.fn.extend.find @ jquery-2.1.4.min.js:2n.fn.init @ jquery-2.1.4.min.js:2n @ jquery-2.1.4.min.js:2(anonymous function) @ dodaj:1460n.event.dispatch @ jquery-2.1.4.min.js:3r.handle @ jquery-2.1.4.min.js:3

what i tried

The error pops out in console for both jQUery 2.14 and 3.0beta1 Just to test for silly mistakes and exclude potential causes (such as jQUery affecting the original variable) , I created two variables with identical content: lnk and lnk2.

Thank you for your suggestions.

3 Answers 3

3

Please try this

$('#confirmation_URL').keyup(function () {
        if (  ($(this).val().length >=5) && ($(this).val().substr(0, 5) != 'http:') && ($(this).val().substr(0, 5) != 'https') ) {
            $(this).val('http://' + $(this).val());
        }
    });
Sign up to request clarification or add additional context in comments.

3 Comments

I simplified it: ` if ( ($(this).val().length >=5) && ($(this).val().substr(0, 4) != 'http') ) { $(this).val('http://' + $(this).val()); console.log('prefixed successfully'); } else { console.log('already prefixed'); }` But now upon pasting a link without http the IF condition always execudes the part which SHOULD be executed when link has a http
I just tried to reverse the IF condition, but I got http://http:// as an output. Funny, but despiriting. Any guesses?
OK. This code finally worked: ` var lnk2 = $('#confirmation_URL').val(); if ( ($(this).val().length >=5) && ($(this).val().substr(0, 4) == 'http') ) { console.log('already prefixed'); } else { $(this).val('http://' + lnk2); console.log('prefixed successfully'); } var lnk = $(this).val();` Thank you all!
1

Try create RegExp object first

regex = new RegExp('^http([s]?):\/\/.*')
if (lnk && !regex.test(lnk)) { 

   lnk.val('http://' + lnk2);
   console.log( "prefix http added successfully" );
}

4 Comments

your solution seems to be a php code. Are you sure I can youe your snippet to define a RexExp in jQuery? Anyway, I still get Uncaught TypeError: lnk.val is not a function
I googled and it was for jQuery. Reading your error, it means the error is not on the regex
My understanding is that the pair 'http://' + lnk2 should be both a text string, but somehow they don't. Still no idea how to fix it. Check out the @Anbarasi solution - within it now the problem is that the IF condition is reversed.
Ok, problem solved by Anbarasi's workaround. Thank you for your attempt!
1
var lnk = $('#confirmation_URL').val();
var lnk2 = $('#confirmation_URL').val();

//adding http if not present
// if (lnk && !lnk.match(/^.+:\/\/.*/)) {  // produces jQuery error
if (lnk && !lnk.match(/^http([s]?):\/\/.*/)) { // produces jQuery error
   // Here produces error actually
   // lnk = $('http://' + lnk2);

   lnk = 'http://' + lnk2;
   $('#confirmation_URL').val(lnk);
   console.log( "prefix http added successfully" );
}

Updated, sorry for the mistake.

4 Comments

this solution produces Uncaught TypeError: lnk.val is not a function. I have also tied $(lnk).val('http://' + lnk2); and other variants, but to no success.
Ok, problem solved by Anbarasi's workaround. Thank you for your attempt!
It seems that you are mixing jQuery objects and strings, $('#confirmation_URL') is a jQuery object, while 'http://' + ... is a string, $('http://' + ...) just doesn't make sense.
This is what I wrote under Aminah Nuraini's solution. I know it doesn't make sense. How transform the jQuery object into a string?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.