I am using formik with yup to validate a form. In one input it passes if it contains only spaces, which is not valid for my use-case. So I went through Google to find some kind of regex so if the string only has spaces it would throw some message, but I didn't find anything.
nome: string()
.min(3, '* Deve conter no mínimo 3 caracteres')
.required('* Este campo é obrigatório')
.matches(/[^\s*].*[^\s*]/g, '* This field cannot contain only blankspaces'),
The problem with this is that when it reaches 3 characters even with required yup condition the validation doesn't work, and matches with regex don't block the spaces. So far I managed a work-around with:
nome: values.nome.trim().replace(/\s+/g, ' ')
But with the correct regex expression I can throw the error in real time.
/^\S+$/
if you need to match a whole string that has no whitespace – Wiktor Stribiżewspace.next === someCaracter ? valid : notValid
logic and this "space next" would be looked by some regex express – cristiano soares