0

I have to build up a NodeJS/React website. The website is all about users posting their events. So while the user is adding his event he has an option to add registration link of the event (link to the third party website where they can directly apply). Adding registration link is handled simply with the input field. Where I ask for the link:

const AddEvent = () => {
  return <input type="text" name="registrationLink" />;
};

Where I show the link:

const EventInfo = props => {
  const { registrationLink } = props; // i.e this outputs https://www.google.com

  return <a href={registrationLink}>Register Now</a>;
};

How can I now validate input to only accept the links (i.e only links with https)? Is there any easy way to do this? Best!

Raghul SK
  • 1,256
  • 5
  • 22
  • 30
Majorka
  • 5
  • 4

3 Answers3

1

You should validate input field before submitting. if it is not valid link you should inform user.

You can check this answer to check validation of url

Check if a JavaScript string is a URL

mfe
  • 1,158
  • 10
  • 15
  • I have tried first answer that is top voted but its not returning me false when string is without https. Intresting. – Majorka Jul 02 '20 at 15:06
  • I recommend to use second answer. check only if protocol 'https' – mfe Jul 02 '20 at 15:10
0

You can use the URL webApi.

https://developer.mozilla.org/en-US/docs/Web/API/URL/URL

If the string passed to the constructor is not a valid URL format, it will throw an error that you can catch.

Yoann Picquenot
  • 640
  • 10
  • 26
0

I have found an answer in one of the topics, I just want to check wheter this is the best way to check the url.

  function isValidURL(string) {
    var regex = /(http|https):\/\/(\w+:{0,1}\w*)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%!\-\/]))?/;
    if (!regex.test(string)) {
      return false;
    } else {
      return true;
    }
  }

Please let me know! Best!

Majorka
  • 5
  • 4