1

I have been looking for way how I could filter data within textarea using regex function for a quite a while now without any success. Below is the regex I want to use to filter UK telephone numbers.

(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?

Fiddle: https://jsfiddle.net/qdypo04y/

I want to achieve the result when the button is clicked it will remove lines which do not meet the regex? Alternatively would remove values which are not UK telephone numbers.

Any guidance would be appreciated.

Tom Jacob
  • 107
  • 1
  • 10
  • The question is why do you use a textarea to enter a number? Using an input element is more appropriate. – Casimir et Hippolyte Jul 28 '17 at 13:31
  • what is a UK number format? As far as i understand it is just `+44` prefix and 10 digits which could be arbitrary separated by sapces and probably '-'. You seems to assume that numbers started with '0' are also UK numbers. Can you define the formats you are looking for? – Serge Jul 28 '17 at 13:36
  • @Serge I assume the regex comes from [here](https://stackoverflow.com/questions/11518035/regular-expression-for-uk-based-and-only-numeric-phone-number-in-cakephp) – gaetanoM Jul 28 '17 at 13:41
  • @gaetanoM you assumption is correct – Tom Jacob Jul 28 '17 at 13:43
  • @CasimiretHippolyte In actual software its extracted using JSON and inserted into text area. – Tom Jacob Jul 28 '17 at 13:44

2 Answers2

1

Apart the use of textarea element your issue is:

An example is:

document.querySelector('button').addEventListener('click', function(e) {
    var txtArea = document.querySelector('textarea[rows="4"][cols="50"]');
    var re = /(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?/;
    var txtArr = txtArea.textContent.split('\n');
    txtArr.forEach(function(ele, idx) {
        txtArr[idx] = ele + '  test result is: ' + re.test(ele);
    });
    txtArea.textContent = txtArr.join('\n');
});
<textarea rows="4" cols="50">
+447222555555
0800 042 0213
2017/07/14
2017/07/17
2017/07/27
</textarea>
<button>Click me</button>
gaetanoM
  • 41,594
  • 6
  • 42
  • 61
0
<script>
function myFunction() { 
    var regexp = /(((\+44\s?\d{4}|\(?0\d{4}\)?)\s?\d{3}\s?\d{3})|((\+44\s?\d{3}|\(?0\d{3}\)?)\s?\d{3}\s?\d{4})|((\+44\s?\d{2}|\(?0\d{2}\)?)\s?\d{4}\s?\d{4}))(\s?\#(\d{4}|\d{3}))?/;
    var content=$.trim($("textarea").val()).split('\n');
    var result="";
    for(var i = 0;i < content.length;i++){
        if(regexp.test(content[i])){
            result=result+content[i]+'\n';          
        }
    }
    $("textarea").val(result);
}

</script>

used JQuery to take the value from textarea

Sarath
  • 608
  • 4
  • 12
  • I would require JQuery within my software for this, is it possible you could also attach a demo? as I cant get it to work as it displays "]]}" – Tom Jacob Jul 28 '17 at 14:19