Hendrix
Hendrix

Reputation: 179

Jquery validate multiline textarea

I have the following textarea, I would like to know how to validate that each line has 13 numbers, correct example:

9999155509775
9999161918929
9999162842704
9999165375551
9999163358495

Incorrect example:

9999155509775
18929 (X)
9999162842704
        (X)
9999163358495

After validating I think it should be something like, ignoring those errors or incorrect formats:

9999155509775
9999162842704
9999163358495

But I would remove blanks or empty lines breaks, there would be some way?

$('#enter').keyup(function () {
    var eachLine = $(this).val().split('\n');
    var result='';
    console.log(eachLine);
    for(var i=0;i<eachLine.length;i++){
        var url = 'http://www.domain.com/myimages/' + eachLine[i] + '/' + eachLine[i] + '_small.jpg';
        result = result + url + '\n';
    }        
    $('#result').html(result);
});

EXAMPLE: http://jsfiddle.net/zj0hwsq2/

Upvotes: 0

Views: 570

Answers (1)

Evan OJack
Evan OJack

Reputation: 577

Check if the line has 13 numbers. If it does, add it to the result. Put this inside the for loop.

for(var i=0;i<eachLine.length;i++){ if (eachLine[i].match(/^[0-9]{13}$/)) { var url = 'http://www.domain.com/myimages/' + eachLine[i] + '/' + eachLine[i] + '_small.jpg'; result = result + url + '\n'; } }

Upvotes: 1

Related Questions