Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Blocking repeated characters 1

Status
Not open for further replies.

struth

Programmer
Aug 26, 2001
114
GB
How can I stop people entering repeated characters etc in forums, etc.

EG
JJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJ endlessly
or
help!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! endlessly

I think it must be looking for the space character at lest every 20 characters or so but don't want to block allowed url addresses

Any ideas

TIA

Struth

Away from the actual ... everything is virtual
 
Posting is a string that is submitted by the user. RepeatCheck is the maximum number of times a character can be repeated. RepeatedCharacters is either 0 or 1. It starts as 0 (post is OK) and if a character is repeated it will be set to 1.

I have tested this and it seems to work although it could be optimized!


<%

Posting="This is a test"
Length=Len(Posting)

RepeatCheck=5
RepeatedCharacters=0

For a=1 to Length-RepeatCheck

Match=0

For b=1 to RepeatCheck

If MID(Posting,a,1)=MID(Posting,a+b,1) Then Match=Match+1

If Match=RepeatCheck Then RepeatedCharacters=1

Next

Next


If RepeatedCharacters=0 Then
Response.Write("This post is OK")
Else
Response.Write("This post has repeated characters")
End If

%>
 
Your best bet is to do it client-side. To do it with ASP you would have to submit the data before you could process it to look for the repeated characters.
With Javascript you could test every time they press a key, when they leave that field, before they submit the form, etc. You would still want to test with ASP as well but you should validate your data at both ends.

First you have to determine the rules for your filter. As you said you could look to ensure a space occurs every 20 characters or so, but you could also test inside that 20 characters to ensure there are a minimum number of distinct characters, or in other words ensuring the 20 characters before the space are not the same character repeated over and over.
But you have to determine how you want to test the data before anyone can give even more than a general idea of an approach since any rule you apply to testing the data can dramatically change the approach.


Stamp out, eliminate and abolish redundancy!
 
Type too slowly and someone will always beat you to an answer. :)

Of course using something like emozley shows above you will probably want to allow for repeated characters but set an upper limit for how many times a characters might repeat within a given length of the data you are testing.


Stamp out, eliminate and abolish redundancy!
 
Thanks, emozley, I have installed this script and it is already doing the job but I take theniteowl's point that it would be better to have the validation happening both ends to make it less of a blunt instrument.

So if I set the rules as being
max length of any given word = 25 characters
max repetition of any given character within those words = 5

How could this be done with javascript?

TIA
Struth



Away from the actual ... everything is virtual
 
struth, this is really a question for the javascript group.
Here is some code to do the testing. If you have questions on it post in the Javascript forum and I will help you there.
Code:
<html>
<head>
<script language="javascript">

function checkDups()
{
  // Get value from text box and split into an array.
  var myString = document.getElementById('myText').value;
  var myArray = new Array();
  myArray = myString.split(' ');
  var cnt;

  //Loop through all array elements
  for (var x=0;x<myArray.length;x++)
  { 
    // Only test if the array element is greater than or equal to 20 characters.
    if (myArray[x].length >= 20)
    { 
      // Test each character in the string.
      for (var y=0;y<myArray[x].length;y++)
      { 
        var testindex = 0;
        var testchar = myArray[x].charAt(y);
        cnt=0;
        // Loop through string counting number of current test character.
        while (testindex != -1)
        {
          testindex = myArray[x].indexOf(testchar, testindex+1);
          cnt++;
          // If maximum number reached then break out of function returning error.
          if (cnt > 5)
          {
            alert("There were more than 5 " + testchar + "'s in the string");    
            return false;
          }
        }
      }
    }
  }
  alert("No dups greater than 5");
  return true;
}
</script>
</head>
<body>
<textarea id="myText" cols="80"></textarea>
<br>
<input type="button" value="Test" onclick="checkDups()">
</body>
</html>


Stamp out, eliminate and abolish redundancy!
 
You can do the same thing in javascript with a regular expression. This code doesn't tell you WHICH character is repeated, but it works. The second form will even catch repeated blank lines, which are actually a repeated pair of characters. Right now it's set for 3 or more repeated characters, but that can be adjusted by changing the value in {2,} to one less than the maximum you allow.
Code:
   var dupCharRE = /(.)\1{2,}/gm;
   var dupCharOrBlankLineRE = /((.)|(\r\n))\1{2,}/gm;
   //Usage:
   if ( dupCharRE.test(yourString) ) {
      alert("You cannot have 3 or more repeated characters");
   }

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
I was hoping someone would say that. Good post tsdragon

 
tsdragon, I am not real solid on regular expressions.
It appears to me that your expression above is set to check on periods or carriage return\line feed combos to separate each area to test for repeats?

And is it looking for the total number within a section of the string or only how many occur in a row?


Stamp out, eliminate and abolish redundancy!
 
I'll see if I can explain what the regular expression does. Firstly, the 'm' flag on the end is the "multiline" flag. It helps the re match when there are multiple lines (i.e. carriage returns and/or linefeeds) in the string.

Now for the regular expression itself: [tt]/((.)|(\r\n))\1{2,}/[/tt]

[tt]/([!](.)[/!]|(\r\n))\1{2,}/[/tt] The dot is the metacharacter to match ANY one character. The parens around it save the character matched for later reference.

[tt]/((.)|[!](\r\n)[/!])\1{2,}/[/tt] Matches a carriage return followed by a line feed (which is how DOS-type files encode a new line), and the parens save the match for later reference.

[tt]/[!]([/!](.)[!]|[/!](\r\n)[!])[/!]\1{2,}/[/tt] The vertical bar '|' is an "OR", so this matches either the single character OR the new line. The parens around this group it for later reference.

[tt]/((.)|(\r\n))[!]\1[/!]{2,}/[/tt] This is a backreference. It returns the value of the FIRST (counting from innermost and leftmost sets of parens) saved match (remember I said we were saving the matched value for later reference). Since we have an OR of two possible values wrapped in parens, the \1 will return either the single character matched, or the CrLf.

[tt]/((.)|(\r\n))\1[!]{2,}[/!]/[/tt] The part is a "quantifier". It says to find the previous match a minimum of two times. There is no maxiumum number of times given, so it will eat up the entire repeated set.

So what we now have is "any character or a CrLf, followed by that SAME character or CrLf two or more times. In other words, three or more occurences of any single character or CrLf combination.

I believe you can find out WHICH character is repeated by using RegExp property $1, but you might have to use match instead of test for that.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Thanks all, particularly those who gave code examples. Tracy's code was a particular thing of beauty and worked like a treat - and many thanks for the explanation.

Tek-tips does it again. I shall raise a question in the javascript forum and point people to this thread.

Away from the actual ... everything is virtual
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top