SEO Software
Closed Thread
Results 1 to 9 of 9

Thread: Validating URL address

  1. #1

    Default Validating URL address

    I want to validate my textbox for website url using javascript. Please give me code or code snippet for it so I can use it. Is there any other way to validate it than the javascript?

    Please advice.


  2. #2

    Default

    Validation should not be done with Javascript because a user can disable Javascript if he or she wants to - validation should be processed server-side.

    However it is possible to do so with Javascript.

    Code:
    var input = document.getElementById("myfield");
    var url_match = /https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/;
    var result = url_match.test(input);
    The variable "result" either shows true or false, whether the input source is a valid URL.

  3. #3
    Fresh Meat Zoli is on a distinguished road Zoli's Avatar
    Join Date
    Jan 2009
    Location
    Veszpr?m, Hungary
    Posts
    29
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default

    Quote Originally Posted by nikon View Post
    I want to validate my textbox for website url using javascript. Please give me code or code snippet for it so I can use it. Is there any other way to validate it than the javascript?

    Please advice.
    Code:
    function isValidURL(url){
        var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
        if(RegExp.test(url)){
            return true;
        }else{
            return false;
        }
    }
    source >> i could not insert it

    If I were you I would get the function run at onblur event.

  4. #4

    Default

    Just keep in mind that such things are better off being done server-side (like with PHP) because users can turn off Javascript if they want to.

  5. #5
    PromoJunkie Staff Kay has a reputation beyond repute Kay has a reputation beyond repute Kay has a reputation beyond repute Kay has a reputation beyond repute Kay has a reputation beyond repute Kay has a reputation beyond repute Kay has a reputation beyond repute Kay has a reputation beyond repute Kay has a reputation beyond repute Kay has a reputation beyond repute Kay has a reputation beyond repute Kay's Avatar
    Join Date
    Dec 2008
    Location
    US
    Posts
    2,055
    Thanks
    158
    Thanked 187 Times in 131 Posts
    Blog Entries
    11

    Default

    Good point, mburt. I know my son uses NoScript for FireFox all the time. He's always on about how great it is.

    Please press the "Thanks" Button in posts you like, don't post to say "Thanks".

  6. #6

    Default

    Your son, does web design run in the family? haha thats awesome :P

  7. #7
    Fresh Meat Zoli is on a distinguished road Zoli's Avatar
    Join Date
    Jan 2009
    Location
    Veszpr?m, Hungary
    Posts
    29
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Default

    Of course, validating a post via server side language is crucial, but truning off the javascript is not so good idea in my opinion, because a lot of function of various websites will be blocked this way.
    Regards
    Zoli

  8. #8

    Default

    there are many code.. one of them is....

    function checkURL(value) {
    var urlregex = new RegExp("^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([0-9A-Za-z]+\.)");
    if(urlregex.test(value))
    {
    return(true);
    }
    return(false);
    }

    if above regular expression does not work then...

    change the regx to " ^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}[0-9A-Za-z\.\-]*\.[0-9A-Za-z\.\-]*$ "


    try it..!!

  9. #9

    Thumbs up

    Quote Originally Posted by Zoli View Post
    Code:
    function isValidURL(url){
        var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
        if(RegExp.test(url)){
            return true;
        }else{
            return false;
        }
    }
    source >> i could not insert it

    If I were you I would get the function run at onblur event.
    thanks zoli.....
    thanks for telling in a good and effective way.....
    all doubts are cleared now......
    thanks.....

Closed Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts