My attempt at validating URLs is encountering a problem. It consistently fails after the input reaches the 6th letter. Even when I type in "www.google.com," which is listed as a valid URL, it still fails to pass the validation.
For example:
w
ww
www
www.
www.g
www.go <-breaks here
www.goo <- continues to break
const validUrl = (value: string | null): string => {
let item: string
const regex = /^(([a-zA-Z]{1})|([a-zA-Z]{1}[a-zA-Z]{1})|([a-zA-Z]{1}[0-9]{1})|([0-9]{1}[a-zA-Z]{1})|([a-zA-Z0-9][a-zA-Z0-9-_]{1,61}[a-zA-Z0-9]))\.([a-zA-Z]{2,6}|[a-zA-Z0-9-]{2,30}\.[a-zA-Z]{2,3})+(\.[a-zA-Z]{2,3})?((\/)?[a-z0-9-]{1,61}(\/)?){0,30}$/
if (value === null || value === '') {
return ''
}
if (value && typeof value === 'string' && !regex.test(value)) {
for (item in urlStrings) {
if (item.startsWith(value)) {
return ''
} else if (value.startsWith(item)) {
return ''
}
}
}
return 'Invalid url'
}
This is urlStrings:
export const urlStrings: { [key: string]: string } = {
'www.costco.com': '',
'www.walmart.com': '',
'www.google.com': '',
'www.facebook.com': '',
}