I have been using the code below to verify that a user input url matches the format www.example.com without the “https://” being mandatory as it is for the default url user input form field.
let emailRegExp = new RegExp(/(http(s)?:\/\/.)?(www\.)[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}([-a-zA-Z0-9@:%_\+.~#?&//=]*)/, "gi");
$w('#url').onChange(() => {$w('#url').onCustomValidation((value, reject) => {
console.log("value: " + value);
let result = emailRegExp.test(value);
console.log(result);
console.log(emailRegExp.test(value));
if (emailRegExp.test(value) !== true) {reject("Please enter a valid url");} else {reject("URL is valid")}
})});
In the console when imputting www.google and then www.google.com once each I get the following:
As you can see the second check runs twice and my variable set to “emailRegExp.test(value)” has a different value to the “console.log()” of the exact same thing.
EDIT: Also note that the reject popup always says “Please enter a valid url” so it actually doesn’t work and prevents me from doing what I want to do.
p.s. I know this is a discussion but if anyone knows another method of doing the same thing I’d love to hear it.
I’ve been testing more and I’ve found the root of the issue:
using the following code
let description = /www\.[-a-zA-Z0-9@:%._\+~#=]{1,5}/gi;
$w('#url').onChange(() => {$w('#url').onCustomValidation((value, reject) => {
console.log("description.test(value): " + description.test(value));
console.log("description.test(value): " + description.test(value));
console.log("description.test(value): " + description.test(value));
console.log("description.test(value): " + description.test(value));
console.log("description.test(value): " + description.test(value));
})});
I get an output of this in my console
description.test(value): true Line 39
description.test(value): false Line 40
description.test(value): true Line 41
description.test(value): false Line 42
description.test(value): true Line 43
This genuinely blew my mind. I don’t think I’ve ever seen anything like it in any programming language. Please fix it because its really annoying.
My fix that I came up with that took me far too long to come up with was just this:
$w('#url').onChange(() => {$w('#url').onCustomValidation((value, reject) => {
let result1 = description.test(value);
let result2 = description.test(value);
if (result1 || result2) {
console.log("Success!!");
} else {
reject("Please enter a valid url");
console.log("Please enter a valid url");
}
})});
So I record it twice and if either is positive then its a success.
This works because both will fail if they dont match the RegExp.