+ Start a Discussion
vino2vijayvino2vijay 

How to write Validation for URL ?

Hello Salesforce Experts,

 

Anyone please let me know how to write valition rule for URL ?

Else someone please provide regular expression string for URL ?

 

Thanks,

Best Answer chosen by Admin (Salesforce Developers) 
Satish_SFDCSatish_SFDC

A small syntax change. 

The following rule worked for me.

 

if(REGEX(URLfield__c,"^((http|https)://)??(www[.])??([a-zA-Z0-9]|-)+?([.][a-zA-Z0-9(-|/|=|?)??]+?)+?$"),false,true)

 

Hope this helps.

 

Regards,
Satish Kumar

Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.

All Answers

Satish_SFDCSatish_SFDC
Can you give an example of how the URL looks like?

Following REGEX can be used for a website address.

^((http|https)://)??(www[.])??([a-zA-Z0-9]|-)+?([.][a-zA-Z0-9(-|/|=|?)??]+?)+?$

Regards,
Satish Kumar
vino2vijayvino2vijay
Hi Sathish,
Please refer the below logic for validation rule, please let me know anything wrong,

if(REGEX("^((http|https)://)??(www[.])??([a-zA-Z0-9]|-)+?([.][a-zA-Z0-9(-|/|=|?)??]+?)+?$",URLfield__c),false,true)

Thanks
Satish_SFDCSatish_SFDC

A small syntax change. 

The following rule worked for me.

 

if(REGEX(URLfield__c,"^((http|https)://)??(www[.])??([a-zA-Z0-9]|-)+?([.][a-zA-Z0-9(-|/|=|?)??]+?)+?$"),false,true)

 

Hope this helps.

 

Regards,
Satish Kumar

Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.

This was selected as the best answer
vino2vijayvino2vijay
Thanks.. Its working fine now .. i am using below code :

if(REGEX(URLfield__c,"^((http|https)://)??(www[.])??([a-zA-Z0-9]|-)+?([.][a-zA-Z0-9(-|/|=|?)??]+?)+?$"),false,true)

Thanks
vino2vijayvino2vijay
ha ha ha ha :) Simultaneously we both replied ;)
Satish_SFDCSatish_SFDC

Yes.

But great....it works now...

 

 

Regards,
Satish Kumar

Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.

jesse1.393977588133575E12jesse1.393977588133575E12
Why use the IF function if REGEX already returns a boolean value?
Kevin Zuiker 12Kevin Zuiker 12

Here is the result of using the following formula: if(REGEX("^((http|https)://)??(www[.])??([a-zA-Z0-9]|-)+?([.][a-zA-Z0-9(-|/|=|?)??]+?)+?$",URLfield__c),false,true)
 

Some seemingly incorrect values are still accepted (e.g., http://www.gfh.") but most of it is okay.

validate website results

Nicholas SartorNicholas Sartor
I was having some troubles with the accepted solution above.

This worked best for me, with trailing slashes, and things with query params and fragments. e.g. https://wwww.classpack.webnode.es/ciao/m?coa=cia&ci=boa#example
^((http|https)://)??(www.)??([a-zA-Z0-9-])+?(.??[a-zA-Z0-9]+?[-&#/?=]??)+?$
What I find offputting is that the proposed solutions works perfectly in regex101 with Java flavor, but wouldn't work in Salesforce.
 
Rohan Kumar 78Rohan Kumar 78

Another problem that's not supposed to be solved with regular expressions...