Mr P!nk Posted September 21, 2007 Share Posted September 21, 2007 all's i want is to know can i set this to pop up an alert when the user gets to 10 then to 0 and disallows them to continue after they reach zero. currently it just goes onto -1, -2 -3 ect ect ect i would be very grateful if someone could help me =] Thank you P!nk <script> function CountWordsLeft(myForm, field, count, no_words) { var text=field.value + " "; if(no_words>0) { var iwhitespace = /^[^A-Za-z0-9]+/gi; var left_trimmedStr = text.replace(iwhitespace, ""); var na = rExp = /[^A-Za-z0-9]+/gi; var cleanedStr = left_trimmedStr.replace(na, " "); var splitString = cleanedStr.split(" "); var word_count = splitString.length -1; count.value=no_words-word_count; } } </script> <form name="count_form" method="post" action=""> <p> <textarea name="desc" cols="40" rows="7" id="desc" onKeyDown="CountWordsLeft(count_form, document.count_form.desc, document.count_form.count,200);" onKeyUp="CountWordsLeft(count_form, document.count_form.desc, document.count_form.count,200);" onPaste="CountWordsLeft(count_form, document.count_form.desc, document.count_form.count,200);"> </textarea> <br> <span class="style4">You have <input readonly name="count" type="text" size="3" value="200"> words left till you reach your limit </span></p> </form> P.S this isnt my code, i grabbed it from an online tutorial, and i cannot remember where i got it, so i cant mail the author about it. Link to comment https://forums.phpfreaks.com/topic/70165-solved-i-know-bugger-alllittle-about-js/ Share on other sites More sharing options...
kael.shipman Posted September 21, 2007 Share Posted September 21, 2007 This looks like a lot of wasted breath to me (the code; not the question). Unless I'm completely missing the point, I think it could be done in a much cleaner way. Please, if you would, explain what the end goal is when the user types in the textarea. Link to comment https://forums.phpfreaks.com/topic/70165-solved-i-know-bugger-alllittle-about-js/#findComment-352678 Share on other sites More sharing options...
Mr P!nk Posted September 24, 2007 Author Share Posted September 24, 2007 I just want to restrict them from putting to much text in a certain area, If you have a suggestion for a "cleaner" code then please be my guest. thanks p!nk Link to comment https://forums.phpfreaks.com/topic/70165-solved-i-know-bugger-alllittle-about-js/#findComment-353924 Share on other sites More sharing options...
kael.shipman Posted September 24, 2007 Share Posted September 24, 2007 Alright, try this: <script type="text/javascript"> function overLimit(el,lim) { if (el.value.length > lim) return false; else return true; } </script> <body> <textarea onkeydown="return overLimit(this,50);"></textarea> </body> I'm not completely sure it's cross-browser, but it would knock my socks off if it wasn't. Incidentally, I made the function so you can send it any element/limit pair. You can use it on as many text inputs as you want with as many different limits as you want. Think scalability! Link to comment https://forums.phpfreaks.com/topic/70165-solved-i-know-bugger-alllittle-about-js/#findComment-354466 Share on other sites More sharing options...
Mr P!nk Posted September 25, 2007 Author Share Posted September 25, 2007 whey hey that worked a treat, when fiddled around and used with some existing code i already had, i now have exactly what i need. So thank you for your help kael.shipman most appreciated (Y) Link to comment https://forums.phpfreaks.com/topic/70165-solved-i-know-bugger-alllittle-about-js/#findComment-355011 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.