Find repetition of characters with .repetition()

.repetition()


Description: Validate if a field contains a character that is repeated consecutively more than allowed times.


Example:

    <!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>repetition demo</title>
      <script src="http://code.bitsycode.com/bitsycode-latest.min.js"></script>
    </head>
    <body>
        
        <table cellpadding="10" cellspacing="0" class="blocks">
            <tr>
                <td width="100" valign="top"><b>Repetition:</b></td>
                <td>
                    <input type="text" name="repetition_field" class="repetition_field" value="BitsyCooode" />
                    <span class="repetition_field_result"></span>
                </td>
            </tr>
            <tr>
                <td></td>
                <td><input type="button" name="submit" onclick="repetition();" value="Validate" /></td>
            </tr>
        </table>
        
        <p><strong>Preview (Multiple Elements):</strong></p>
        <table cellpadding="10" cellspacing="0" class="blocks">
            <tr>
                <td width="100" valign="top"><b>Repetition:</b></td>
                <td>
                    <input type="text" name="repetition_field_1" class="repetition_field_1" value="BitsyCooode" />
                    <span class="repetition_field_1_result"></span>
                </td>
            </tr>
            <tr>
                <td width="100" valign="top"><b>Repetition:</b></td>
                <td>
                    <input type="text" name="repetition_field_2" class="repetition_field_2" value="BitsyCoode" />
                    <span class="repetition_field_2_result"></span>
                </td>
            </tr>        
            <tr>
                <td></td>
                <td><input type="button" name="submit" onclick="repetitionboth();" value="Validate Both Fields" /></td>
            </tr>
        </table>        
        
        <script>
            
            function repetition() {
                
                var repeated = bitsyCode(".repetition_field").repetition({ min:2 });
                repeated = repeated ? "&#x2718 Repetition <b>found</b>." : "&#x2714; <b>No</b> repetition.";
                bitsyCode(".repetition_field_result").html(repeated);
                
            }
            
            function repetitionboth(){
                
                var repeated = bitsyCode(".repetition_field_1, .repetition_field_2").repetition({ min:2 });
                
                rep1 = repeated[0] ? "&#x2718 Repetition <b>found</b>." : "&#x2714; <b>No</b> repetition.";
                rep2 = repeated[1] ? "&#x2718 Repetition <b>found</b>." : "&#x2714; <b>No</b> repetition.";
                
                bitsyCode(".repetition_field_1_result").html(rep1);
                bitsyCode(".repetition_field_2_result").html(rep2);
               
            }
            
        </script>
     
    </body>
    </html>

Preview:

Repetition:

Preview (Multiple Elements):

Repetition:
Repetition:
  • Usage

    • How to use:

      Validate elements for repetition of characters

    • Single element:

      Validation on a single element:

      bitsyCode(".repetition_field").repetition();
    • Multiple elements:

      Validating multiple elements, comma separated: eg. (#element-id , .element-class)

      bitsyCode(".repetition_field_1, .repetition_field_2").repetition();
  • Options

    • What options are there:
      max: 2 (default value) // Specify maximum amount of times a charater can consecutively repeat before becoming repetition.
    • Example usage:
      bitsyCode(".repetition_field").repetition({ max:3 });
  • Returns

    • Returns:

      There are 2 types of returns.

    • Single element:
      Return: boolean (true or false)   // Returns true if repetition is found otherwise false is returned.
    • Multiple elements:
      Return: [ ]   // Returns an array, of true or false in element sequence.

      Example returned object:

      Array [ true, false ]
Share