You need to sign in to do that
Don't have an account?

URL validation through apex
Since the field type URL cannot check for the data whether it is really a URL or not, I want to ask for how to write a controller for check the URL when submiting. Thank you for all your work!
All Answers
As an alternative, Since you already have the data in the URL field, create a validation rule for checking then REGEX on it.
REGEX code can be found below :
Here is my code:
Thank you so much!
I asked you to create a formula field (return type checkbox) and then use this REGEX there.
Secondly write a validation rule, checking for this formula, which if false should return you a validation error.
Error: Function REGEX may not be used in this type of formula
How can I solve it?
Just copy paste the above in a validation rule (In formulas REGEX is not supported), not need for a formula.
Do mark my answer as a best answer if it helped.
You need to do an exception handling to get the error on the page in a handled way rather than beraking the page something like below: It's better to do it in validation rule, cos what if someone from back-end inserts value, at that time you won't be able to catch the wrong url.
Let me know if that still didn't work
Errors
Facebook: Please enter a valid URL.
Error : Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please enter a valid URL.: [Facebook__c]
The first statement is mine and I do not want the second statement come out. How can I solve this problem?
Something like : Do mark my answer as best answer if it helped you!
By the way, can you do a favour for me? When we reach the end of the question, can you gather the information and I would choose that one so that other users can get help from it. Thank you.
My original idea for the URL field is not a required field. However, under your suggestion, these fields are forced to be required in logic. Since we set the formula for the validation in the field, if user can choose not to insert, it become a null value and does not pass the validation rule.
Any other method can perform validation check in apex code value, but not in field setting value? Thank you very much.
Hi I am looking for the SF validation rule for below code.Can someone help.. Ref link: https://stackoverflow.com/questions/37013313/how-to-check-if-valid-facebook-url-before-submit/37013467#37013467
function validateUrl() {
url = $("#FacebookUrl").val();
var pattern = /^(?:(?:http|https):\/\/)?(?:www.)?facebook.com\/(?:(?:\w)*#!\/)?(?:pages\/)?(?:[?\w\-]*\/)?(?:profile.php\?id=(?=\d.*))?([\w\-]*)?$/;
if(pattern.test(url)) {
alert("Correct URL");
}
else {
alert("Incorrect URL");
}
return pattern.test(url);
}