Javasript - Url validation

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Javasript - Url validation

Post by Shendemiar »

Does anyone have a ready code to validate if the given string is a valid url ?
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

Found one with cryptic REGEXP

Could someone clean the crap out of this:

Code: Select all

var expression2 = new RegExp("^(((ht|f)tp(s?))\://)?(www.|їa-zA-Z].)їa-zA-Z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk|fi)(\:ї0-9]+)*(/($|їa-zA-Z0-9\.\,\;\?''\\\+&%\$#\=~_\-]+))*$", "i");
I'd like it to have just

[http(s)://][something]

with the legal characters.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

something like this (untested).

Code: Select all

var expression2 = new RegExp("^(https?\://)?їa-zA-Z0-9-]+\.(їa-zA-Z0-9-]+\.?)+/?($|їa-zA-Z0-9\.\,\;\?''\\\+&%\$#\=~_\-]+)$", "i");
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

REGEXP seems fine (http://www.regexlib.com/Search.aspx?k=url%20validate) But i can't get my javasript working. Nothing seems to match.


Code: Select all

function checkURL(url)
{
    var expression = new RegExp("^(http://)?їa-zA-Z0-9-]+\.(їa-zA-Z0-9-]+\.?)+/?($|їa-zA-Z0-9\.\,\;\?''\\\+&%\$#\=~_\-]+)$", "i");

    if (expression.test(url))
        return true;
    else
        {
        alert( "Kotisivun osoite ei ole oikein.");
        return false;
        }
}
Wait. I gave it a wrong parameter...
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

Now it accepts all except an empty line
Shendemiar
Forum Contributor
Posts: 404
Joined: Thu Jan 08, 2004 8:28 am

Post by Shendemiar »

Ok now it works

Thanks
Post Reply