www.webdeveloper.com
Results 1 to 4 of 4

Thread: Preventing urls containing specified words ?

  1. #1
    Join Date
    Jan 2006
    Posts
    2

    Preventing urls containing specified words ?

    Is there a way of preventing url's containing specified words being submitted in a html form ?

    i.e. if a user attempts to submit a form with the word "casino" anywhere in the url field, a message is returned.

    At the moment I only have a check that the field cannot be empty as shown below..

    if (document.addurl.url.value == "") {
    alert("Please specify a url")
    document.addurl.url.focus();
    return false;

    Cheers
    Matt

  2. #2
    Join Date
    Nov 2002
    Location
    England
    Posts
    693
    You can use indexOf("casino") to check for the word...

    I'd use an array and an iterator ie:

    words[0]="casino";
    words[1]="poker";
    words[2]="cards";

    for(var i=0; i<words.length; i++){

    if(document.addurl.url.indexOf(words[i]) != -1){
    alert("Invalid url");
    return false;
    }

    }

  3. #3
    Join Date
    Jan 2006
    Posts
    2
    Thanks for the info. Im having problems using it in this funciton as follows...

    [I]<script language="javascript">

    function valData () {

    words[0]="casino";
    words[1]="poker";
    words[2]="cards";

    for(var i=0; i<words.length; i++){

    if(document.addurl.url.indexOf(words) != -1){
    alert("Invalid url");
    return false;
    }
    if (document.addurl.website.value == "") {
    alert("Please specify your website name")
    document.addurl.website.focus()
    return false
    }
    if (document.addurl.recip_url.value == "") {
    alert("Please specify where your reciprocal link url will be")
    document.addurl.recip_url.focus();
    return false;
    }
    }
    </script>
    <SCRIPT TYPE="text/javascript">
    <!--
    function checkEmail(email)
    {
    if(email.length > 0)
    {
    if (email.indexOf(' ') >= 0)
    alert("email addresses cannot have spaces in them");
    else if (email.indexOf('@') == -1)
    alert("a valid email address must have an @ in it");
    }
    }
    //-->
    </SCRIPT>


    Any ideas?

  4. #4
    Join Date
    Jan 2005
    Posts
    3,067
    just use the .match method:
    Code:
    var myurl = document.addurl.url.value; 
    if (myurl == "") {
    alert("Please specify a url")
    document.addurl.url.focus();
    return false;
    }
    if(myurl.match('casino'))
    {
    alert('That is an invalid URL')
    return false;
    }
    And if you want to check for multiple words then just put them inside of an array:
    Code:
    <script type="text/javascript">
    function changeThem()
    {
    var mywords = new Array('casino','cards','poker');
    var myurl = document.getElementById('ads').value.toLowerCase(); 
    var i, word;
    if (myurl == '') 
    {
    alert("Please specify a url")
    document.addurl.url.focus();
    return false;
    }
    
    for(i=0;i<mywords.length;i++)
    {
    if(myurl.match(mywords[i]))
    {
    alert('That is an invalid URL. The word ' + mywords[i] + ' cannot be used in the URL.');
    return false;
    }}}
    just add whatever words you want to the array. In the script I changed every input to lowercase so that you don't need multiple inclusions for one word.
    Last edited by konithomimo; 01-10-2006 at 06:34 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles