function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ 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...
oscar clarkoscar clark
Here is a regular expression for validating URLs:
^(https?|ftp)://(-\.)?([^\s/?\.#-]+\.?)+([^\s]*)?$
This regular expression allows URLs starting with either "http", "https", or "ftp", and checks for valid domain names as the gbwa.download is for GB whatsapp (https://gbwa.download/) and any additional path or query parameters. Note that this regular expression is not foolproof and may not catch all variations of valid URLs.
In terms of writing a validation rule for a specific programming language, it will depend on the language you are using. In general, you can use the regular expression above and apply it to the input using the language's regular expression matching function. For example, in JavaScript:
const urlRegex = /^(https?|ftp)://(-\.)?([^\s/?\.#-]+\.?)+([^\s]*)?$/;

function validateUrl(url) {
  return urlRegex.test(url);
}

In this example, validateUrl is a function that takes a string input url and returns a boolean indicating whether the input matches the URL regular expression.
Fredu CarlosFredu Carlos
One way to validate a URL is by using regular expressions. Here is a sample regular expression string for a URL:
^(https?|ftp)://[^\s/$.?#].[^\s]*$
This regular expression will match URLs that start with "http://", "https://", or "ftp://", followed by any characters that are not whitespace, "/", "?", "#", or ".". This will ensure that the URL is properly formed and does not contain any invalid characters.
lipix maryalipix marya
It is one of the best site that I have visited. Hope you will share more quality blog posts thank you.
Kahoot Login Info (https://kahoot-account.blogspot.com/2023/07/fun-and-interactive-how-kahoot-online.html" target="_blank)
Atiq MuhammadAtiq Muhammad

To create a validation rule for URLs in Salesforce, you can use a combination of Salesforce's built-in functions and regular expressions. Here's an example of how you can write a validation rule to ensure that a URL field is in the correct format:

AND(
  NOT(ISBLANK(Your_URL_Field__c)), 
  NOT(REGEX(Your_URL_Field__c, "^(http|https)://[a-zA-Z0-9./?=_-]+$"))
)
In this validation rule, replace Your_URL_Field__c with the API name of the URL field you want to validate. The regular expression used here checks if the URL starts with "http://" or "https://" and contains valid characters commonly found in URLs.
Now, if you are looking for a regular expression string for URLs, you can use the following one:

^(http|https)://[a-zA-Z0-9./?=_-]+$
Make sure to include this regular expression within appropriate code tags when posting in the Salesforce forum.
Lastly, for more information or assistance, you can visit my site at GB WhatsApp (https://gbwaapk.net). It's a great resource for downloading the latest and updated virus-free version of GB WhatsApp, which is completely free and Anti-Ban.
Hope this helps! Let me know if you have any further questions.

Today SochToday Soch
The easiest way to perform URL validation in JavaScript is by using the new URL constructor function. In addition to its simplicity, it’s supported by the Node.js runtime and most browsers.

The basic syntax is as follows:

new URL (url)
new URL (url , base)
JavaScript only requires the base element if we provide a relative URL Todaysoch (https://www.todaysoch.com/). If we don’t provide one, it defaults to undefined. Alternatively, if we provide a base element with an absolute URL, JavaScript ignores the base element.
Eric Smith 86Eric Smith 86
I always wanted to learn to code. I want to make a website that automatically shows gaming results such as teer game result (https://shillong-teer-result.com/), lottery sambad result (https://nagalandlotterysambad.live/) and dear lottery result (https://dear-lottery.in/). Can anyone help?