Skip to content Skip to sidebar Skip to footer

Validate That A URL Entered In A Form Is From A Specific Website With Javascript

I have a website where people post links from Google+. I am trying to make sure that people can only post specific links from Google plus. An example would be, someone would need t

Solution 1:

edit edit: you have two problems you need to reference the bonuslink value not the DOM element itself and you need to call it as a member of the 'theForm' instead of 'form' since that is what you called the parameter. Other than that everything should be fine.

<script type="text/javascript" language="JavaScript"> 
function checkForm(theForm) {
    if (theForm.bonuslink.value.indexOf("https://plus.google.com/games/907809777960/params/") == -1){ 
        alert('You can only enter authentic Google + links'); 
        return false; 
    } else { 
        return true;
    }
}
</script>
<form action="submitbonus.php" onsubmit="return checkForm(this);" method="post">
 Bonus Link: <input name="bonuslink" type="text" size="40" />  <input name="Submit" type="submit" value="Submit Bonus" /><br />
</form>

Solution 2:

this regular expression will extract the domain name from any string. basically it'll return the part starting from http:// etc.

/((https?|s?ftp|dict|www)(://)?)[A-Za-z0-9.\-]+)/gi

it'll detect the following forms:

enjoy.


Post a Comment for "Validate That A URL Entered In A Form Is From A Specific Website With Javascript"