The following warnings occurred:
Warning [2] Undefined variable $unreadreports - Line: 26 - File: global.php(961) : eval()'d code PHP 8.2.25 (Linux)
File Line Function
/global.php(961) : eval()'d code 26 errorHandler->error
/global.php 961 eval
/printthread.php 16 require_once



UserSpice
Jquery url validation / Allow only specific urls - Printable Version

+- UserSpice (https://userspice.com/forums)
+-- Forum: Support Center (https://userspice.com/forums/forumdisplay.php?fid=23)
+--- Forum: UserSpice 4.3 and Below (https://userspice.com/forums/forumdisplay.php?fid=26)
+--- Thread: Jquery url validation / Allow only specific urls (/showthread.php?tid=1349)



Jquery url validation / Allow only specific urls - Vtins - 02-19-2019

Hi
how can i allow only links from specific urls?

"https://www.youtube.com/watch?v="

so user can only add youtube video urls ? Huh

jquery validation plugin have the rule "url"
but thats not what i need..

any ideas for me ?


RE: Jquery url validation / Allow only specific urls - mudmin - 02-19-2019

(02-19-2019, 10:25 PM)Vtins Wrote: Hi
how can i allow only links from specific urls?

"https://www.youtube.com/watch?v="

so user can only add youtube video urls ? Huh

jquery validation plugin have the rule "url"
but thats not what i need..

any ideas for me ?

What if instead of having them give the whole link, they just give the code...  Like if the video is https://www.youtube.com/watch?v=0sr1Xeocuuc have them just submit 0sr1Xeocuuc and you do the rest when you echo it back out?


RE: Jquery url validation / Allow only specific urls - Vtins - 02-19-2019

i have a url form input where users cann add youtube videos.

if users try to add this url i need to echo a validation error
"https://youtube.com/channelname"

echo a validation error too on this example
"https://otherdomain.com"



i need to allow only values thats contain youtube video urls like this
"https://www.youtube.com/watch?v="



But users needs to type the fully youtube url ... not only the video id


RE: Jquery url validation / Allow only specific urls - mudmin - 02-19-2019

(02-19-2019, 10:38 PM)Vtins Wrote: i have a url form input where users cann add youtube videos.

if users try to add this url i need to echo a validation error
"https://youtube.com/channelname"

echo a validation error too on this example
"https://otherdomain.com"



i need to allow only values thats contain youtube video urls like this
"https://www.youtube.com/watch?v="



But users needs to type the fully youtube url ... not only the video id

I made a quick demo for you. You can save it in users/whatever.php and then run that file.  https://pastebin.com/01x5GZuT

Essentially what you need to do is give your form an id of "myform"  and your url input an id of "url"  Then add this javascript to the bottom of the page.

Code:
<script type="text/javascript">
$( document ).ready(function() {
   $('#myform').submit(function (e) {
           var url = $("#url").val(); //get the url from the input by the id url
           console.log(url);
           var lowercase = url.toLowerCase(); //temporarily convert to lowercase to avoid issues
           if(lowercase.startsWith('https://www.youtube.com/watch?v=') == false && lowercase.startsWith('https://m.youtube.com/watch?v=') == false) {
               e.preventDefault();
               alert("Invalid youtube url.  Must be https://www.youtube.com/watch?v=xxxxxxx.");
           }
   })

});
</script>

Again, there's a full demo if you want to see it in action.


RE: Jquery url validation / Allow only specific urls - astropos - 02-20-2019

I'm also collecting YouTubes but I'm doing it similarly to how Mudmin suggests above - only accepting the video id rather than trying to navigate the numerous forms of a YT url (there's also https://youtu.be/xyzxyzxyz from the 'share' section).

You could create a YT search facility, here's mine: https://youtu.be/gxcz83OFB1g Note line 79 is one of many options.


RE: Jquery url validation / Allow only specific urls - mudmin - 02-20-2019

(02-20-2019, 10:35 AM)astropos Wrote: I'm also collecting YouTubes but I'm doing it similarly to how Mudmin suggests above - only accepting the video id rather than trying to navigate the numerous forms of a YT url (there's also https://youtu.be/xyzxyzxyz from the 'share' section).

You could create a YT search facility, here's mine: https://youtu.be/gxcz83OFB1g Note line 79 is one of many options.

ah yeah. I forgot about youtu.be 
Smile