Have a textarea:

<textarea cols="20" id="destinationAddress" name="DestinationAddresses" rows="2"></textarea> 

It is allowed to enter values ​​in the following formats: 79168859376 , 79168859376, 79163324345 , 79168859376, 79163324345, 79163324345
How to use jQuery to validate to allow input in this format, also to check the number, the initial number is 7, the remaining ten are from 0 to 9?

  • Only one 11-digit number or many 11-digit numbers separated by ... comma? - user207618 2:41 pm
  • There may be a lot of 11-digit numbers separated by commas, and maybe one number - S.Ivanov

2 answers 2

You can cut out all the allowed (numbers in a valid format, commas and spaces) and check whether something is left?
If yes - in the form of extraneous characters, we derive an error.
If not - fine, then there was only approved from Above, the form is valid.

Something like this:

 let textarea = document.querySelector('#destinationAddress'), check = document.querySelector('#check'), log = document.querySelector('#log'); check.addEventListener('click', e => { let value = textarea.value.replace(/(?:7\d{10}|,(?!,)|\s+)/gi, ''); log.style.color = value !== '' ? 'red' : 'green'; log.innerHTML = value !== '' ? 'Bad news' : 'Passed!'; }); 
 <textarea cols="20" id="destinationAddress" name="DestinationAddresses" rows="2"></textarea><br /> <input type='button' id='check' value='Проверить!' /><br /> <span id='log'></span> 

  • '79168859376 ,, ,,,,,,,,,,,,,,,,,,,,,,,,, 79168859376, 79163324345, 79168859376, 79163324345 ,, ,,,,,,,,,,,,,,,, , 79163324345 ,,,, 'here such an expression passes. Draining your regulars, try in my example. - Victor Zharikov
  • @ Viktor Zharikov, the question does not say about commas in a row. Although if the author will need, I can easily fix. - user207618
 text=$('#destinationAddress').val(); arr=text.split(','); re = /(?:7\d{10}|,|\s+)/gi; //Не силён в регулярках. Надо чуть чуть улучшить под ваш запрос arr.forEach(function(item, i, arr) { if (!re.test(item)){ return false; } });