Jump to content

[SOLVED] i know bugger all/little about js


Mr P!nk

Recommended Posts

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.