Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-25-2012, 03:39 PM   PM User | #1
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,191
Thanks: 217
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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

Quote:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''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.


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


Quote:
'''''''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 04:17 AM..
durangod is offline   Reply With Quote
Old 09-25-2012, 03:52 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,461
Thanks: 52
Thanked 457 Times in 455 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
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;
xelawho is offline   Reply With Quote
Users who have thanked xelawho for this post:
durangod (09-25-2012)
Old 09-25-2012, 04:12 PM   PM User | #3
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,191
Thanks: 217
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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
durangod is offline   Reply With Quote
Old 09-25-2012, 05:08 PM   PM User | #4
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,191
Thanks: 217
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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;
durangod is offline   Reply With Quote
Old 09-25-2012, 05:15 PM   PM User | #5
WolfShade
Regular Coder

 
Join Date: Apr 2012
Location: St. Louis, MO, USA
Posts: 960
Thanks: 7
Thanked 100 Times in 100 Posts
WolfShade is an unknown quantity at this point
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".
WolfShade is offline   Reply With Quote
Users who have thanked WolfShade for this post:
durangod (09-25-2012)
Old 09-25-2012, 06:08 PM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
durangod (09-25-2012)
Old 09-25-2012, 11:40 PM   PM User | #7
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,191
Thanks: 217
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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-25-2012 at 11:42 PM..
durangod is offline   Reply With Quote
Old 09-26-2012, 12:38 AM   PM User | #8
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,191
Thanks: 217
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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 04:16 AM..
durangod is offline   Reply With Quote
Old 09-26-2012, 08:19 AM   PM User | #9
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 09-26-2012, 09:17 AM   PM User | #10
durangod
Senior Coder

 
Join Date: Nov 2010
Posts: 1,191
Thanks: 217
Thanked 31 Times in 30 Posts
durangod is on a distinguished road
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
durangod is offline   Reply With Quote
Old 09-26-2012, 12:17 PM   PM User | #11
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,103
Thanks: 197
Thanked 2,421 Times in 2,399 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:20 AM.


Advertisement
Log in to turn off these ads.