How to detect (in a textarea or others inputs) if the user has finished to enter a url as Facebook does in its main form please ?
Solutions like this work but only when the user has finished typing and performs an action, eg clicking a button.
I will wish to detect urls throughout the entry, eg run a check on the word that has been typed after each space.
Thank you in advance.
How to detect (in a textarea or others inputs) if the user has finished to enter a url as Facebook does in its main form please ?
Solutions like this work but only when the user has finished typing and performs an action, eg clicking a button.
I will wish to detect urls throughout the entry, eg run a check on the word that has been typed after each space.
Thank you in advance.
Share Improve this question edited May 23, 2017 at 12:09 CommunityBot 11 silver badge asked Aug 23, 2012 at 13:57 ncrocferncrocfer 2,5704 gold badges33 silver badges38 bronze badges 1- The identification of URLs would be the same, you'd just be binding it to a different event - a key being pressed rather than a button being clicked. – Anthony Grist Commented Aug 23, 2012 at 14:03
3 Answers
Reset to default 8Here is an example on how you could do this:
$(function() {
"use strict";
var url = /[-a-zA-Z0-9@:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9@:%_\+.~#?&//=]*)?/gi;
$("#textarea").on("keyup", function( e ) {
var urls, output = "";
if ( e.keyCode !== 8 && e.keyCode !== 9 && e.keyCode !== 13 && e.keyCode !== 32 && e.keyCode !== 46 ) {
// Return is backspace, tab, enter, space or delete was not pressed.
return;
}
while ((urls = url.exec(this.value)) !== null) {
output += urls[0] + ", ";
}
console.log("URLS: " + output.substring(0, output.length - 2));
});
});
Of course you would have to bind other events like .blur()
for it to work properly.
Try the following fiddle and check you console as you type: http://jsfiddle/sQVe/mXQrc/1/
Look at this: https://github./stephan-fischer/jQuery-LiveUrl/ - its clean, simple, and provides a preview of the url!
What you are looking for is the keypress event, you can view the documentation and an example of how to use it here: http://api.jquery./keypress/