Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,446
    Thanks
    275
    Thanked 32 Times in 31 Posts

    validating input

    Hi i been playen around with some javascript to try to do some extra validation to my form.

    Issue: someone was too lazy to type anything in the textarea so they just
    typed in

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''hello
    to reach their 120 char min requirement.

    what i came up with is not that great so i thought i would ask here.

    Here is what i have

    Code:
    function chk_validchars()
     {
    
    var field=document.getElementById("mytext").innerHTML; 
    var newfield=str.replace("''''''''''","invalid character");
    document.getElementById("mytext").innerHTML=newfield;
    
     }
    
    </script>



    Here is the out put (certainly not what i want) I thought maybe it would at least put invalid character on every occurance of the length of ' but it just does it once.


    invalid character''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''hello
    Prefered would be either replace with null or even something like this


    '''''''i am an Idiot''''''i am an idiot'''''''''''''''''''''''''hello

    lol sorry just had to be mean on that one, but seriously if we could replace multiple chars of 4 or more like '''''' or ............. with some kind of text like invalid would be great.


    120 chars required is only three lines of short text and hard to believe people cant come up with that.
    Last edited by durangod; 09-26-2012 at 05:17 AM.

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,989
    Thanks
    56
    Thanked 557 Times in 554 Posts
    you could try this, which replaces 4 or more of any consecutively repeated character with "invalid character"...

    Code:
    var str=document.getElementById("mytext").innerHTML; 
    var newfield=str.replace(/(\w)\1{3,}/g," invalid character ");
    document.getElementById("mytext").innerHTML=newfield;

  • Users who have thanked xelawho for this post:

    durangod (09-25-2012)

  • #3
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,446
    Thanks
    275
    Thanked 32 Times in 31 Posts
    Thanks yes that replaces stuff like nnnnnnnnnnn or aaaaaaaaa

    but it does not replace ........ or ''''''''''''' or ;;;;;;;;;;;

    is there a tweek for that?

    Thanks so much

  • #4
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,446
    Thanks
    275
    Thanked 32 Times in 31 Posts
    found this expression and it does seem to work but it puts invalid character a bunch of times rather than just once, which i guess is ok but i would like the option of just once if possible.

    Code:
    var str=document.getElementById("mytext").innerHTML; 
    var newfield=str.replace(/[^\w\s]|(.)(?=\1{3,})/gi," invalid character ");
    document.getElementById("mytext").innerHTML=newfield;

  • #5
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    The one you found will replace anything that isn't alphanumeric or space.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • Users who have thanked WolfShade for this post:

    durangod (09-25-2012)

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,253
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Code:
    <div id = "mytext">
    </div>
    
    <script type = "text/javascript">
    var field =  ' """"""i am an Idiot"""""i am an idiot"""""""hello '
    if(/(.)\1{2,}/i.test(field)) {   //string contains at least 3 identical consecutive characters (case insenstive)
    alert ("Three in a row in the string!");
    field = field.replace(/(.)\1{3,}/gi,"");
    document.getElementById("mytext").innerHTML = field;
    }
    
    </script>
    But that does not stop qwertyuiopqwertyuiopwertyuipqwertyuipqwertyuiop

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    durangod (09-25-2012)

  • #7
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,446
    Thanks
    275
    Thanked 32 Times in 31 Posts
    thanks phil, that was awesome, perfect... that will at least stop them from the repeat char input... Peace very cool... If i give you another thanks i would, reply and i will give you a bonus thanks if you want.

    only one thing, i think this should be

    field = str.replace(/(.)\1{3,}/gi,"");

    not

    field = field.replace(/(.)\1{3,}/gi,"");
    Last edited by durangod; 09-26-2012 at 12:42 AM.

  • #8
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,446
    Thanks
    275
    Thanked 32 Times in 31 Posts
    Nevermind i got it, my eyes were playing tricks on me, after i took a break i found the issue. THanks all
    Last edited by durangod; 09-26-2012 at 05:16 AM.

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,253
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Quote Originally Posted by durangod View Post
    thanks phil, that was awesome, perfect... that will at least stop them from the repeat char input... Peace very cool... If i give you another thanks i would, reply and i will give you a bonus thanks if you want.

    only one thing, i think this should be

    field = str.replace(/(.)\1{3,}/gi,"");

    not

    field = field.replace(/(.)\1{3,}/gi,"");
    No. Why do you say that?

    You can only give one "Thanks" per post, but you can add to my rep if you want!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #10
    Senior Coder
    Join Date
    Nov 2010
    Posts
    1,446
    Thanks
    275
    Thanked 32 Times in 31 Posts
    sorry my eyes were playing tricks on me i convinced myself the command was str.replace instead of replace.. oops Its all good, just fyi i had to add an if statment before that process to check for null or value or even space because it was seeing the empty textarea as repeated chars. So it would cause the alert to go off prematurely.

    Nice job thanks

  • #11
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,253
    Thanks
    203
    Thanked 2,557 Times in 2,535 Posts
    Quote Originally Posted by durangod View Post
    sorry my eyes were playing tricks on me i convinced myself the command was str.replace instead of replace.. oops Its all good, just fyi i had to add an if statment before that process to check for null or value or even space because it was seeing the empty textarea as repeated chars. So it would cause the alert to go off prematurely.

    Nice job thanks
    Huh? Three consecutive spaces, yes. No spaces or an empty field - no.
    Note that the value of a form field can never be null. Only "" (blank).

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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