Daikin
Daikin

Reputation: 11

Validate specific url

I'm making a form for my friend that required all visitor to fill a specific domain name. What I want is, they've to write a link starting with https://specificname.com and continue with an ID. Example https://specificname.com/id/WordOrNumber . I need something like regex or validator to validate the link they've fill in the form.

This is the string that I am using for now.

case 'url':
    if ('http://' === $value) $value = '';
    if ($value && !preg_match('/^(https?|s?ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\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|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(([a-z]|\d|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])*([a-z]|\d|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])))\.)+(([a-z]|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(([a-z]|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])*([a-z]|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:|@)|[\x{E000}-\x{F8FF}]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/iu', $value))
        return frmd_error('Please enter a valid URL.', $elm['name']);
    break;

This code work perfectly but it will accept all kind of domain name that the visitor fill up. Please just reply with the regex or validator only because I'll not know what to edit .

I'm bad with coding.

Upvotes: 0

Views: 202

Answers (2)

gurvinder372
gurvinder372

Reputation: 68393

if you prefix and input url is

var validPrefix = "validDomain.com";

var inputURL = "www.validDomain.com/context1";

then all you need to do is

if ( inputURL.indexOf( validPrefix ) == -1 )
{
   alert( "No, input URL doesn't contain valid domain prefix" );
}
else
{
   alert( "Yes, input URL contains valid domain prefix" );
}

Upvotes: 0

kicken
kicken

Reputation: 2167

If you want to ensure the URL entered matches a specific prefix, then you can skip the regular expressions and just test the substring, such as:

var validPrefix = 'https://example.com';
if (enteredUrl.substring(0, validPrefix.length) != validPrefix){
    alert('Invalid URL.');
}

If you want to allow for minor variations on the URL such as with/without the WWW or http and https, then you can use a simple regex that defines those as optional parts.

var urlRegex = /https?:\/\/(www.)?example.com/;
if (!urlRegex.test(enteredUrl)){
    alert('Invalid URL.');
}

If you need to validate the entire URL format rather than just the prefix, the regex will need to be extended based on whatever format criteria you need to match. Some regular expression tutorials should help you determine how to match what you need. To add an ID number after the domain for example you could do:

var urlRegex = /https?:\/\/(www.)?example.com\/\d+/;
if (!urlRegex.test(enteredUrl)){
    alert('Invalid URL.');
}

Upvotes: 1

Related Questions