Error404 Error404 - 1 year ago 108
Javascript Question

validate the character count of comma separated words in textarea

I am trying to validate the value which is entered on a text area, whether the user have entered the valid ten digit number within the comma separated values like


9876543210,98766543211,98766543212


if the users entered more than or less than 10 digits of number have to alert some error.

so far i have tried something like

$("#myMso").keyup(function(){
var value = $(this).val().replace(" ", "");
var word = value.split(",");

var lengths = words.map(function(word) {
if(lengths != 10){
alert("Hey! some of the contact number you have entered is not valid");
}
});
});

Answer Source

Try like this

   $("#myMso").keyup(function(){
        var value = $(this).val();
        var word = value.split(",");
        $.each(word,function (key,value) {
            if(value.length != 10){
                alert("Hey! some of the contact number you have entered is not valid");
            }

        });
     }); 

Modification for on-change event:-

 $("#myMso").on('change',function(){
            var value = $(this).val();
            var word = value.split(",");
            $.each(word,function (key,value) {
                if(value.length != 10){
                    alert("Hey! some of the contact number you have entered is not valid");
                }

            });
         }); 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download