0

My textarea can count string/character when user enter input. Here is my jsfiddle.

My problem is I want set condition mean when string/character is 158 above, I want show message inside <div id="message">We will deduct 2 credit from your account</div> and same when string/character is 316 above the message will update and show<div id="message">We will deduct 3 credit from your account</div> and continue ..

Example:

158 *2

158 = We will deduct 2 credit from your account
316 = We will deduct 3 credit from your account
474 = We will deduct 4 credit from your account
632 = We will deduct 5 credit from your account
...

5 Answers 5

2

Try it

$('#myInput').keyup(function() {
    $('#charCount').text(this.value.length);

    var c = parseInt(this.value.length / 158);
    if(c > 0) 
        $('#message').text('We will deduct '+c+' credit from your account');
});​
Sign up to request clarification or add additional context in comments.

1 Comment

but why when i delete string/character the message not update ?
0

1) find length of String

2) Divide it by 158

3) Add '1' to the answer

4) Create message using String concatenation - "We will deduct" + output of (3) + "credit from your account"

5) jQuery("#message").html("We will deduct" + output of (3) + "credit from your account");

Comments

0

Try this

$('#myInput').keyup(function() {
    var len = this.value.length ; 
    $('#charCount').text(len);
    var creditCount = 158;
    if( len > creditCount ){
         var credits = Math.floor(len / creditCount );
         $('#message').html('We will deduct '+  credits  + ' credit from your account;' )
    }
});​

Check DEMO

2 Comments

There's a typo in your demo at Line 4. Maybe the code you expect is "var creditCount = 158;"?
Its not a typo actually.. Was testing my code with just a smaller input :) .. Fixed that
0

on textarea keyup you can check no. of characters presents and accordingly you can write if-else conditions to display the message.

check this:working fiddle

$('#myInput').keyup(function() {
    if(this.value.length<=158)
    $('#charCount').text('We will deduct 1 credit from your account');
    else if(this.value.length>158 || this.value.length<316)
    $('#charCount').text('We will deduct 2 credit from your account');
    else if(this.value.length>316|| this.value.length<316)
    $('#charCount').text('We will deduct 3 credit from your account');
});​

Comments

0
$('#myInput').keyup(function() {
    $('#charCount').text(this.value.length);
    var length = this.value.length;
    if (length % 158 == 0) {
      $('#message').html('We will deduct '+ (length/158) + 
                         ' credit from your account;' )                        
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.