0

How do I return a warning pop-up if someone enters a url or start typing ‘http’?

My code below only pop up warning if someone starts typing http in the box, but not if someone paste a whole url in the box.

<p>Enter http on the keyboard in the input field, to alert some text.</p>

<input type="text" size="50" onkeydown="keyCode(event)"> 

<script>
function keyCode(event) {
    var x = event.keyCode;
    if (x == 80) {
        alert ("Pleas do not enter a url!");
    }
}
</script>
Katherine
  • 39
  • 6
  • Take a look at this, https://stackoverflow.com/questions/3809401/what-is-a-good-regular-expression-to-match-a-url. If there's a match for a url, then trigger your alert otherwise just let them continue. –  Sep 27 '18 at 10:15
  • 1
    I would listen for the `change`, `blur`, `mouseup` and `keyup` events (there are a lot of ways to modify input fields) and run a function which searches for the text you want to detect (http) using something like `.indexOf()` – DBS Sep 27 '18 at 10:15

3 Answers3

2

Your code checks if someone presses p is all.

To do it correctly you need to check contents of input with oninput=yourfunction() instead.

It would be easier if you gave it an id <input ... id="this_input">, then you can access it with var v = document.getElementById("this_input").value;, but you can access it by tag as document.getElementsByTagName("input")[0].value; where [0] is index of your input on the page.

Then you search for a URL in that string with v.search("[some_regexp]");, it returns -1 if it doesn't find a URL. So it's if(v.search("...") >= 0) alert("There is a URL in the input somewhere.");

And lastly depending on what you think is a URL the regexp would be different, the most paranoid one would check if there're 2 words separated by a dot.

Here it is: ((http|ftp|https):\/\/)?([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?

It's unnecessarily long, but you can google a shorter one for your exact purpose. For example to find if there's an http somewhere in the string you just do (http), which doesn't dtop people from posting links, like www.google.com.

Complete example would be:

<p>Enter http on the keyboard in the input field, to alert some text.</p>

<input type="text" size="50" oninput=check()> 

<script>
function check() {
    var v = document.getElementsByTagName("input")[0].value;
    if(v.search("(http)") >= 0)
        alert("woot");
}
</script>
IcedLance
  • 406
  • 3
  • 12
0
<input type="text" size="50" onkeyup="keyCode(this)"> 

Javascript:

function keyCode(el) {
    var iVal = el.value;

  if(iVal.indexOf('http:') != -1 || iVal.indexOf('https:') != -1) {
        alert('stahop');
  }
}

it allways checks if there is http:, no matter how you put it. can be. ggkmhhhttp: it will still find it. you could do && www or simular.

Thomas J.
  • 593
  • 8
  • 21
  • 1
    You can add any criteria like I did there., use `|| iVal.indexOf('neededText') != -1)` what it does, it just checks if there is text string in there. – Thomas J. Sep 27 '18 at 10:37
  • 2
    If you want to have more restrictions : `` You will have to reload or to change page and come back to avoid the alert (**EDIT** as Community said, it is not recommand to use both mousedown/up) – LPK Sep 27 '18 at 10:39
  • 1
    @LPK I wouldnt do that, since on mousedown and up would both drop alerts, that would be annoying for user, it would constantly drop same error, since it would find http both ways. unless to add a check if it all ready dropped error. – Thomas J. Sep 27 '18 at 10:40
  • 1
    you're right, it was just to illustrate DBS's comment. It is indeed annoying for the user ^^ – LPK Sep 27 '18 at 10:41
  • 1
    Yep, well it makes sense that way. – Thomas J. Sep 27 '18 at 10:43
  • 1
    @LPK Im pretty sure mark is tryhard and he just downvoted me lmao – Thomas J. Sep 27 '18 at 10:44
  • Yup, I saw it immediatly haha. Your response is clear and work just fine (I tried with jsfiddle) – LPK Sep 27 '18 at 10:46
  • Yes I downvote because the OP says "My code below only pop up warning if someone starts typing http in the box, but not if someone paste a whole url in the box.". The downvote is because `right-mouse` -> paste does not work with your code, only when use `ctrl` + `v`. It's not about being try hard. It's about helping people get a usable answer. If you change that I have no problem removing my down vote. – Mark Baijens Sep 27 '18 at 11:23
  • 1
    So instead of correcting it, writing in comments what in your opinion is wrong and how it could be solved - like @LPK did. You decide you just going to downvote it, say nothing about it and then fly away. Makes sense bruh, makes sense. :) *If code works, but you think there is something missing, you dont start typing new code. You add stuff*. Hence, my name – Thomas J. Sep 27 '18 at 11:26
  • 1
    Thats not really telling and you said you cant paste stuff which you can with ctrl v, but you didint specify that you cant paste it with right mouse click (Which I was not avare and didint think about), and you even said: Oh sorry it works.. So its like you said that there is a tree somewhere and then delete the message. But oh well, we wont know now, cuz you did delete it. right. – Thomas J. Sep 27 '18 at 11:29
  • I did comment with that. You said the OP only wanted it on keydown so i thought i misread and removed my comment. After reading the OP question again I noticed that you where the one who read it wrong. Since you are so stubborn I just posted my own answer and explanation and didn't bother commenting more. Your name sure serves you right sir. Anyway, I'll move on. Have a good day sir. Hopefully my feedback is good use for you. – Mark Baijens Sep 27 '18 at 11:31
  • 1
    Excuse me. I am stubborn? I misread the thing and you coulve said: "no, you know. you are still wrong, add some blocks of code right there". And you still keep deleting comments, which makes no sense to me. If you mean to write something write it. not post it and delete it. Jesus. And you did comment on that? Idk where. Cant see your comment. Anyways, this is getting outta hand. Good luck in life n all. – Thomas J. Sep 27 '18 at 11:34
0

If you use the input event it will work on all inputs. If you work with really old browsers you might need to change the input event to change in combination with keyup and mouseup

Note that sometimes users will not use http but start with www so be carefull what you define as url.

document.querySelector("input").addEventListener("input", function(){
  if(this.value.indexOf("http") != -1) {
    console.log("works on paste");
  }
});
<input type="text" size="50">
Mark Baijens
  • 13,028
  • 11
  • 47
  • 73