0

I am using a normal jquery validation to validate user input to be a valid URL

so validation for this input is true and a user can input a valid URL

http://www.example.com/investments/stocks-shares/?source=XYZ

However i want the user to be able to enter a URL something like

http://www.example.com/investments/stocks-shares/?source={user-code}

Now here {user-code} makes the URL invalid and the user cannot enter this URL. So i need a REGEX which can validate the URL like this

http://www.example.com/investments/stocks-shares/?source={user-code}

{user-code} can occur anywhere in the url. I am new to REGEX so any help is appreciated. Thanks in advance.

Mohan
  • 4,055
  • 7
  • 31
  • 56
  • 3
    Please show examples of what you want the regex to accept, and what you want the regex to reject. Also, it would be helpful if you show your work, i.e. show us the regex you've come up with so far. – Jonathan Benn Jun 17 '14 at 13:53
  • I Just need a simple URL validator i just need an execption with it that it allows curly braces in the URL. – Mohan Jun 18 '14 at 09:43
  • As a seemingly functional answer has already been given in the meantime, I am wondering why you would want to validate URLs? The examples that you give are perfectly valid, given the implementation of the answer by Jonathan, but are still fake, dead links. What's the use then? – Frank Conijn Jun 19 '14 at 21:20

1 Answers1

1

Please note that it is absolutely not possible to make a perfect URL validator with regex alone. I have worked extensively on URL validation and I can guarantee this to you 100%.

You can make a regex-only URL validator that works for most cases, but it will have some false positives and false negatives.

There is already an answer for a simple validator here by Matthew O'Riordan. I just had to modify his validator to support curly braces.

Here's the modified regex and your answer:

^((([A-Za-z]{3,9}:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_{}]*)#?(?:[\w]*))?)$
Community
  • 1
  • 1
Jonathan Benn
  • 1,533
  • 3
  • 17
  • 26