-2

how can we validate the website url's by using the regex.

Url's like this:

google.com

www.google.com

http://google.com

https://www.google.com

here is we want only support above link. if we enter junk data like www://abc, abcdefg like this are not allowed.

I have tried like this in Drupal:

Here is drupal custom code:

$form['web_url'] = array(
        '#title' => 'Web URL :',
        '#type' => 'textfield',
        '#size' => 100,
        '#default_value' => @$sourcepath,
        '#required' => TRUE,
        '#maxlength' => 100,
        '#attributes' => array('class' => array('myclass_edit')),
  );

function edit_files_form_validation($form, &$form_state){
  $website_url = $form_state['values']['web_url'];
  drupal_set_message("hi");
  drupal_set_message($website_url);
  if(!preg_match("/^(http[s]?)\:\/\/([aZ09-_]*?\.)?([aZ09-_]{2,}\.)([\w\.]{2,5})$" ,$website_url )){

     form_set_error('web_url',t('Please use only an valid URL links')); 

  }

}

But not working as my requirement.

RaMeSh
  • 3,330
  • 2
  • 19
  • 31
  • Cannot understand what you want to acccomplish. Both these url's are valid... – Leonidas Jan 20 '15 at 08:58
  • 1
    There are already too many questions like this and answers. http://stackoverflow.com/questions/161738/what-is-the-best-regular-expression-to-check-if-a-string-is-a-valid-url – Saren Arterius Jan 20 '15 at 08:58

4 Answers4

1

you can try this regex

/^((ht|f)tp(s?)\:\/\/|~/|/)?([w]{2}([\w\-]+\.)+([\w]{2,5}))(:[\d]{1,5})?/

http://www.google.com , 
https://www.google.com,
ftp://www.google.com,
www.google.com

function->

function valid_url($value)
    {
        $pattern = "/^((ht|f)tp(s?)\:\/\/|~/|/)?([w]{2}([\w\-]+\.)+([\w]{2,5}))(:[\d]{1,5})?/";
        if (!preg_match($pattern, $value))
        {
            return FALSE;
        }
        return TRUE;
    }
mintra
  • 337
  • 1
  • 3
  • 19
  • @user as mention by Saren Arterius is correct ,This question already has an answer on SO – mintra Jan 20 '15 at 09:05
  • how can we generate this regex. – RaMeSh Jan 20 '15 at 09:17
  • @user you need to write function for this for example `function valid_url($value) { $pattern = "/^((ht|f)tp(s?)\:\/\/|~/|/)?([w]{2}([\w\-]+\.)+([\w]{2,5}))(:[\d]{1,5})?/"; if (!preg_match($pattern, $value)) { return FALSE; } return TRUE; }` – mintra Jan 20 '15 at 09:21
  • how we can we write this in drupal custom functions – RaMeSh Jan 20 '15 at 09:32
  • @user in php , codeigniter we write call back function – mintra Jan 20 '15 at 09:52
0
^(?:(?:http|https):\/\/)?(?:[\w-]+\.)+[\w]+(?:\/[\w- .\/?]*)?$

You can try this.

$re = "/^(?:(?:http|https):\\/\\/)?(?:[\\w-]+\\.)+[\\w]+(?:\\/[\\w- .\\/?]*)?$/m";
$str = "http://google.com\nhttps://google.com";

preg_match_all($re, $str, $matches);
vks
  • 67,027
  • 10
  • 91
  • 124
  • Is this correct for php functions.i am using like this but not working if(!preg_match("/^(?:(?:http|https)://)?(?:[\w-]+\.)+[\w]+(?:/[\w- ./?]*)?$" ,$website_url )){ form_set_error('web_url',t('Please use only an valid URL links')); } } – RaMeSh Jan 20 '15 at 09:14
0

you can use this regex

/^(http|https)?://[a-zA-Z0-9-.]+.[a-z]{2,4}/

Sunil Pachlangia
  • 2,033
  • 2
  • 15
  • 25
0

Here is my solution :

^(http[s]?)\:\/\/([aZ09-_]*?\.)?([aZ09-_]{2,}\.)([\w\.]{2,5})$

Get the protocol, subdomain (if exists), domain (required), and ext (required).

Rules:

  • http or https required

  • Domains must contains at less 2 chars

  • Ext must contains at less 2 chars and a limit of 5 chars

Examples:

http://tonton.google.fr
1.  [0-4]   `http`
2.  [7-14]  `tonton.`
3.  [14-21] `google.`
4.  [21-23] `fr`

http://www.google.co.uk
1.  [46-50] `http`
2.  [53-57] `www.`
3.  [57-64] `google.`
4.  [64-69] `co.uk`

https://paulrad.com
1.  [70-75] `https`
3.  [78-86] `paulrad.`
4.  [86-89] `com`

Demo: https://regex101.com/r/pV1hX8/2

Paul Rad
  • 4,820
  • 1
  • 22
  • 23