1

I'm trying to determine if a pasted URL is valid. I'm using the bind() function to detect a paste event. I'm using a regex that I found on here for the validation (which works and is fine for the time being). I'm also appending some text to tell the user if it's a valid url or not.

The only thing that isn't working when it's inside the bind() is the URL validation. It works when placed outside of it, though.

JS:

 function validateURL(textval) {
  var urlregex = new RegExp( "^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$");
  return urlregex.test(textval);
}

$(document).ready(function(){   

    // assign whatever is in the inputbox to a variable
    var url = $("#ent").val()

    //this is if they paste the url from somewhere
    $("#ent").bind('paste', function() {

        if(validateURL(url)) {
            $("#ent").css("background-color","green");
            $("#status").append("Valid URL");
            $("#submit").attr('disabled', 'disabled');
        }
    });

});

HTML:

<input type="text" name="ent" id="ent">
<input type="submit" name="submit" id="submit">
<div id="status"></div>
S L
  • 14,262
  • 17
  • 77
  • 116
bob_cobb
  • 2,229
  • 11
  • 49
  • 109
  • 1
    Just a side comment: You can validate the URL by making a request and looking for a 200 OK return code. More reliable than regex. (If its an existing URL) – Elad Lachmi Mar 28 '11 at 07:50
  • @Elad, I'm unfamiliar with this method. I tried to find something on here about it, but couldn't. api.jquery.com is currently down for me as well. – bob_cobb Mar 28 '11 at 08:19
  • You can use jquery.ajax to make a call to the URL and check the status code returned. I would give you a refrence, but jquery.com is down, as you said. – Elad Lachmi Mar 28 '11 at 08:38

4 Answers4

7

you can try this:

$(document).ready(function(){   
     function validateURL(textval) {
  var urlregex = new RegExp( "^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&amp;%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&amp;%\$#\=~_\-]+))*$");
  return urlregex.test(textval);
}
    // assign whatever is in the inputbox to a variable


    //this is if they paste the url from somewhere
    $("#ent").live('input paste', function() {
        var url = $("#ent").val();
        if(validateURL(url)) {
            $("#ent").css("background-color","green");
            $("#status").append("Valid URL");
            $("#submit").attr('disabled', 'disabled');
        }
    });

});

jsfiddle

Dirty-flow
  • 2,306
  • 11
  • 30
  • 49
Sarah West
  • 2,047
  • 2
  • 20
  • 37
1

Hmm, couldn't you write it slightly different? I'm not sure if the $("#ent") exists on document ready, that could stop the behaviour.

$(document).ready(function(){

    //this is if they paste the url from somewhere
    $("#ent").bind('paste', function() {
        // assign whatever is in the inputbox to a variable
        var that = $(this);
        if(validateURL(that.val())) {
            that.css("background-color","green");
            $("#status").append("Valid URL");
            $("#submit").attr('disabled', 'disabled');
        }
    });
});
Johan
  • 3,202
  • 1
  • 23
  • 19
1

user blur event instead.

<script src="jquery.js"></script>
<script>
 function validateURL(textval) {
      var urlregex = new RegExp( "^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&amp;%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&amp;%\$#\=~_\-]+))*$");

  return urlregex.test(textval);
}

$(document).ready(function(){   

    $("#ent").bind('blur',
        function ()
        {
            if(validateURL($(this).val())){
                $('#result').html('Valid url')          
            }
            else{
                $('#result').html('Invalid url')
            }
        }    
    )
    });

</script>
<input type="text" name="ent" id="ent">
<input type="submit" name="submit" id="submit">
<label id="result"></label>
<div id="status"></div>
S L
  • 14,262
  • 17
  • 77
  • 116
  • Thanks. Why is blur better than bind? The reason I was using bind was because I wanted the user to immediately know upon pasting a URL into the input box whether or not it worked. – bob_cobb Mar 28 '11 at 08:20
  • @bob_cobb `bind` is a function, and `blur` is an event. So, using any `events` like `blur` or `keyup`, you can bind events. You were binding `paste` event, which probably does not exits for textbox. – S L Mar 28 '11 at 08:27
0

I am using .change method because .live method not working... code run when you hit enter or tab

$(document).ready(function() {

    function validateURL(textval) {
      var urlregex = new RegExp( "^(http|https|ftp)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&amp;%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&amp;%\$#\=~_\-]+))*$");
      return urlregex.test(textval);
    }

    $('#myurl').change(function(){
        var url = $("#myurl").val();
        if(!validateURL(url)) {
            $("#status").html("enter Valid URL");
        }
        else {
                $("#status").hide();
        }
    }); //event handler
}); //document.ready

jsfiddle