#1
  1. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,216
    Rep Power
    220

    Validate URL with Jquery


    Hi;

    What is missing from this? How can I make it validate properly?

    Code:
    function validate_url(str)   // return true or false.
                                        {
    if(/^(http|https|ftp):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(:[0-9]{1,5})?(\/.*)?$/i.test(str)) 
            {
                return 1;
            } 
        else 
            {
               return false;
            } 
    }
    It accepts this as a valid url:
    Code:
     newthread.php?do=newthread&f=115http://forums.devshed.com/newthread.php?do=newthread&f=115http://forums.devshed.com/newthread.php?do=newthread&f=115http://forums.devshed.com/newthread.php?do=newthread&f=115http://forums.devshed.com/newthread.php?do=newthread&f=115http://forums.devshed.com/newthread.php?do=newthread&f=115http://forums.devshed.com/newthread.php?do=newthread&f=115
  2. #2
  3. Lazy Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,386
    Rep Power
    9645
    That regex won't accept that URL, but the URL I think you're trying to show is valid. It's weird, but valid.

    Besides, perfect validation in a regex is a pain and not worth the effort. The thing you have there looks good enough.
  4. #3
  5. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,216
    Rep Power
    220
    Ok thanks

IMN logo majestic logo threadwatch logo seochat tools logo