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.

<scripttype="text/javascript"language="JavaScript">functioncheckForm(theForm) {
    if (theForm.bonuslink.value.indexOf("https://plus.google.com/games/907809777960/params/") == -1){ 
        alert('You can only enter authentic Google + links'); 
        returnfalse; 
    } else { 
        returntrue;
    }
}
</script><formaction="submitbonus.php"onsubmit="return checkForm(this);"method="post">
 Bonus Link: <inputname="bonuslink"type="text"size="40" /><inputname="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"