Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yup validation of website using url() very strict

Tags:

reactjs

yup

I am trying to validate an input field as a website using

yup.string().url()

But it seems if the protocol is not sent it gives an error, when the website should be flexible to even accept for example stackoverflow.com

like image 557
SImon Haddad Avatar asked May 06 '20 12:05

SImon Haddad


People also ask

How to validate URL without default URL validator?

Instead of using default url validator you can use your own regex. Your code changes like: You can use your own rule for regex and validate url. You can read more about it there. Show activity on this post. A lot of urls break on the verified answer. Something closer to Yup.url () but allowing the omission of http, www. and // would be:

How do I validate an email in Yup?

The Yup object schema provides two methods for validating: isValid and validate. isValid resolves true if the user submits a valid string, and false if an error exists in the string. validate returns an errors object if the input value is an invalid email. The errors object contains either default error messages or your custom validation message.

How to validate form fields using Yup?

First, we will create new schema object with Yup. This schema will define all values (form fields) we want to validate. These values will be firstName, lastName, email, password and website. We will want all these values to be string () and required (). We will specify the email value to match email format, with email ().

What is the best way to validate a web page?

The most basic and also most accessible is the native way. This is the validation provided by browsers. This validation works well if you use correct field types and don’t need any customization. Then, there are bigger, all-in-one solutions, such as Formik. These solutions offer a lot of flexibility and customization.


5 Answers

Adding some more validations to the @trash_dev's regex,

you could try https://regex101.com/r/V5Y7rn/1/

const regMatch = /^((http|https):\/\/)?(www.)?(?!.*(http|https|www.))[a-zA-Z0-9_-]+(\.[a-zA-Z]+)+(\/)?.([\w\?[a-zA-Z-_%\/@?]+)*([^\/\w\?[a-zA-Z0-9_-]+=\w+(&[a-zA-Z0-9_]+=\w+)*)?$/;

Yup
 .string()
 .matches(regMatch, "Website should be a valid URL")

It also considered extra considerations for URL such as:

www.test-my-skills.gov.cz/0999asd-xzc88?0-_/sad%20123/@asdas
asdasd.com/asdasd/asdasd/asdasd/@asasd
https://www.somehow.com/@aasd
https://www.test.facebook.com/@sdas
http://www.computer.com.au/
like image 187
Animesh Singh Avatar answered Oct 19 '22 14:10

Animesh Singh


All the answers treat www.mywebsite as valid. Which should not be the case.

const re = /^((ftp|http|https):\/\/)?(www.)?(?!.*(ftp|http|https|www.))[a-zA-Z0-9_-]+(\.[a-zA-Z]+)+((\/)[\w#]+)*(\/\w+\?[a-zA-Z0-9_]+=\w+(&[a-zA-Z0-9_]+=\w+)*)?$/gm

Yup.string().matches(re,'URL is not valid')

matches:

  • vercel.com
  • www.vercel.com
  • uptime-monitor-fe.vercel.app
  • https://uptime-monitor-fe.vercel.app/
like image 44
trash_dev Avatar answered Oct 19 '22 12:10

trash_dev


Instead of using default url validator you can use your own regex. Your code changes like:

website: Yup.string()
        .matches(
            /((https?):\/\/)?(www.)?[a-z0-9]+(\.[a-z]{2,}){1,3}(#?\/?[a-zA-Z0-9#]+)*\/?(\?[a-zA-Z0-9-_]+=[a-zA-Z0-9-%]+&?)?$/,
            'Enter correct url!'
        )
        .required('Please enter website'),

You can use your own rule for regex and validate url. You can read more about it there.

Play around with it here: https://regex101.com/r/O47zyn/4

like image 31
aturan23 Avatar answered Oct 19 '22 13:10

aturan23


A lot of urls break on the verified answer. Something closer to Yup.url() but allowing the omission of http, www. and // would be:

const URL = /^((https?|ftp):\/\/)?(www.)?(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i

Yup.string().matches(URL, 'Enter a valid url')
like image 11
Justin.Mathew Avatar answered Oct 19 '22 13:10

Justin.Mathew


Just completing @aturan23, you can add a - inside [a-z0-9] and [a-zA-Z0-9#], like this:

((https?):\/\/)?(www.)?[a-z0-9-]+(\.[a-z]{2,}){1,3}(#?\/?[a-zA-Z0-9#-]+)*\/?(\?[a-zA-Z0-9-_]+=[a-zA-Z0-9-%]+&?)?$

You can validate url like this:

  • material-ui.com

  • https://github.com/mui-org/material-ui

  • http://github.com/mui-org/material-ui

  • github.com/mui-org/material-ui/core#teste

like image 6
Cmte Cardeal Avatar answered Oct 19 '22 12:10

Cmte Cardeal