...

View Full Version : Resolved validating input



durangod
09-25-2012, 04:39 PM
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




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.

xelawho
09-25-2012, 04:52 PM
you could try this, which replaces 4 or more of any consecutively repeated character with "invalid character"...



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

durangod
09-25-2012, 05:12 PM
Thanks yes that replaces stuff like nnnnnnnnnnn or aaaaaaaaa :thumbsup:

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

is there a tweek for that? :confused:

Thanks so much

durangod
09-25-2012, 06:08 PM
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.



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

WolfShade
09-25-2012, 06:15 PM
The one you found will replace anything that isn't alphanumeric or space.

Philip M
09-25-2012, 07:08 PM
<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

durangod
09-26-2012, 12:40 AM
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,"");

durangod
09-26-2012, 01:38 AM
Nevermind i got it, my eyes were playing tricks on me, after i took a break i found the issue. THanks all :)

Philip M
09-26-2012, 09:19 AM
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! :)

durangod
09-26-2012, 10:17 AM
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

Philip M
09-26-2012, 01:17 PM
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).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum