RESOLVED - Verify URL with onCustomValidation + regex

Hello, someone could help me, I can not figure out where the error is.

I needed to change the url validation error message, and not knowing how else to make or use the only tool I know onCustomValidation. To do this I came across the regex for the first time, and looking at a few guides and doing a lot of tests I thought this solution worked, but it does not work

$w(“#ImputUrl”).onCustomValidation((value, reject) => {
let UrlVerify = ‘http(?:s)?://boards.4chan.org/([a-z])/thread/([0-9])(?:#[0-9a-z]*)?’ ;
if ( ( value !== UrlVerify ) && (value.length > 0) ) {
$w(“#error02”).text = “Formato URL: http://www.ilMioIndirizzo.com”;
$w(“#error02”).show();
reject(“Formato URL: http://www.ilMioIndirizzo.com”);
} else {
$w(“#error02”).hide();
}
console.log(‘Function Control Field Input Url Work’);
});

where am I wrong?
Thank you

Hey Red,

How are you? Looks like you’re having fun. :smiling_face:

Your code isn’t properly checking the string against the regex expression. It’s more complicated than just comparing:

let UrlVerify = 'http(?:s)?\://boards\.4chan\.org/([a-z]*)/thread/([0-9]*)(?:\#[0-9a-z]*)?' ;
if (  ( value !== UrlVerify ) && (value.length > 0) ) {

You can set the input field as a URL and it will check the validity for you. Would that work for you?


The other option is to enable Add pattern validation and then enter in your regex expression:


Do either of these work for you?

If you really insist on using onCustomValidation, then it would look something like this:

$w("#ImputUrl").onCustomValidation((value, reject) => {
	var regex = new RegExp('http(?:s)?\://boards\.4chan\.org/([a-z]*)/thread/([0-9]*)(?:\#[0-9a-z]*)?') ;
	if (regex.test(value)) {
		$w("#error02").hide();
	} else {
		$w("#error02").text = "Formato URL: http://www.ilMioIndirizzo.com";
		$w("#error02").show();
		reject("Formato URL: http://www.ilMioIndirizzo.com");
	}
});

Note: This is only an example based on your code. I have no idea if the regular expression is a valid pattern, and I haven’t tested the code. I’ll leave that to you :smiling_face:

Just for fun, you can try running this code and see how regular expressions work. To make it easy, just stick it in the page’s onReady() function. You can then play with the string and the expression and see the test results (valid/invalid) in the console.

var string = "sample1";
var regex = new RegExp("^([a-z0-9]{5,})$");
if (regex.test(string)) {
    console.log("valid");
} else {
    console.log("invalid");
}

Well, that oughta keep you off the streets. Have fun,

Yisrael

Hi Yisrael,

I really like programming, a world that fascinates me so much. Sorry if I answer you late, but I was busy and I finally came back to have fun.

I had already tried set the input field and Add pattern validation but these did not allow me to customize the error messages that were shown, or at least I did not see how to do

However your sample code illuminated my mind, I started to play and understand how it works, I also went to this site https://regex101.com/ that I found in another discussion and after 2 hours I managed to do my personalized REGEX, you do not know that huge happiness.

Thank you so much, you are helping me to learn and discover this new world. As soon as I stop I can not wait to start again

Thank you

Daniel