0

I found lots of url validation method in this website and also in others. But i can not find the exact one that is required for my project. Here is what i need.

<form name="example" value="">
 <input type='text' name='link' />
<input type="submit" name="submit" id="submit" value="" />    
 </form>

Now if someone type a link in the text field then i have to check whether the link is valid or not. Valid link will be like

1. http://www.example.com
2. www.example.com
3. example.com
4. (some space) then one of the three links above
5. one of the three links above then some space

These are the valid formats. Anything else is typed in the text field should give output invalid link

I require the solution both in javascript and PHP.

2 Answers2

1

This handles all your cases on php side

$regex = "((https?|ftp)\:\/\/)?"; // SCHEME
        $regex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; // User and Pass
        $regex .= "([a-z0-9-.]*)\.([a-z]{2,3})"; // Host or IP
        $regex .= "(\:[0-9]{2,5})?"; // Port
        $regex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; // Path
        $regex .= "(\?[a-z+&\$_.-][a-z0-9;:@&%=+\/\$_.-]*)?"; // GET Query
        $regex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?"; // Anchor


    //    $url = 'http://www.domain.dk/seo/friendly/url';

        if(preg_match("/^$regex$/", trim($url)))
        {
        print 'true';
        }
Poonam
  • 4,591
  • 1
  • 15
  • 20
-1

you can find this javascript url validation answer with following post

http://stackoverflow.com/questions/1303872/url-validation-using-javascript

For php validation find below the code

function isValidURL($url)
{
return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}

if(!isValidURL($fldbanner_url))
{
$errMsg .= "* Please enter valid URL including http://<br>";
}
Poonam
  • 4,591
  • 1
  • 15
  • 20