Page 1 of 1

Javasript - Url validation

Posted: Tue Nov 09, 2004 1:54 pm
by Shendemiar
Does anyone have a ready code to validate if the given string is a valid url ?

Posted: Tue Nov 09, 2004 2:38 pm
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.

Posted: Tue Nov 09, 2004 2:58 pm
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");

Posted: Tue Nov 09, 2004 4:05 pm
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...

Posted: Tue Nov 09, 2004 4:18 pm
by Shendemiar
Now it accepts all except an empty line

Posted: Tue Nov 09, 2004 4:29 pm
by Shendemiar
Ok now it works

Thanks