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!

1
  • try <input type="url" name="registrationLink" pattern="https?://.+" required /> Commented Jul 2, 2020 at 15:00

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

2 Comments

I have tried first answer that is top voted but its not returning me false when string is without https. Intresting.
I recommend to use second answer. check only if protocol 'https'
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.

1 Comment

Could you mark my answer as the good answer please? Really appreciate :)
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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.