Link to home
Start Free TrialLog in
Avatar of austerhaus
austerhaus

asked on

Filtering strings in multiline textbox

Hello,

I have a form in asp.net c# that contains a multiline textbox. The purpose of the textbox is to allow users to type or paste a list of 10-digit phone numbers (potentially up to 100). The numbers will be in various formats when typed/pasted (i.e. "(555)555-5555", "555-555-5555", "5555555555", etc.). I need a way to "clean" the numbers so that any non-numeric characters are removed and each 10-digit number is separated by a semi-colon (instead of a space, comma, cr, etc.).

Example:
5555555555 555-555-5555, (555)555-5555

would be cleaned to:
5555555555;5555555555;5555555555

Your assistance is greatly appreciated. Please let me know if additional detail is required.

Thanks
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

You can transform my JavaScript example to C#. If not tell me and I will do it for you.
But this is my concept:
<script>
var theValue = "5555555555 555-555-5555, (555)555-5555 555 555 5555";
 
var theList = theValue.replace(/[\(\)\-\,\.\;]+/g,"").replace(/(\d{3})\s*(\d{3})\s*(\d{4})\s*/g,"$1$2$3;").replace(/\;\s*$/,"");
 
alert(theList);
</script>

Open in new window

Sorry, but I have a small improvement:
<script>
var theValue = "115555555555 555-555-5555, (555)555-5555 555 555 5555";
 
var theList = theValue.replace(/[\(\)\-\,\.\;]+/g,"").replace(/\b(\d{3})\s*(\d{3})\s*(\d{4})\b\s*/g,"$1$2$3;").replace(/\;\s*$/,"");
 
alert(theList);
</script>

Open in new window

The extra \b checking was necessary to prevent digits borrow to neigbour when too many or less digits were in one telefon number group.
Avatar of austerhaus
austerhaus

ASKER

Thanks for the fast reply. Unfortunately, I am not as familiar with javascript as I probably should be. Can you put this into a function that I can simply call from an onclick event? Sorry.
Sure:

<textarea rows=7 cols=20 onclick='alert(this.value.replace(/[\(\)\-\,\.\;]+/g,"").replace(/\b(\d{3})\s*(\d{3})\s*(\d{4})\b\s*/g,"$1$2$3;").replace(/\;\s*$/,""))'>
5555555555 555-555-5555, (555)555-5555 555 555 5555
</textarea>

Open in new window

Thanks. Here is how I implemented it:

This seems to work well however, if there are any spaces or CRs at the start of the first line they are not removed. Would you mind changing this?


onclick="document.getElementById('<%=TextBox1.ClientID%>').value = (document.getElementById('<%=TextBox1.ClientID%>').value.replace(/[\(\)\-\,\.\;]+/g,'').replace(/\b(\d{3})\s*(\d{3})\s*(\d{4})\b\s*/g,'$1$2$3;').replace(/\;\s*$/,''));"

Open in new window

Slightly extended:
nclick="document.getElementById('<%=TextBox1.ClientID%>').value = (document.getElementById('<%=TextBox1.ClientID%>').value.replace(/[\(\)\-\,\.\;]+/g,'').replace(/\b(\d{3})\s*(\d{3})\s*(\d{4})\b\s*/g,'$1$2$3;').replace(/(^\s*|[\;\s]*$)/g,'));"

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks. I changed it only slightly to insert "; " (semi-colon[space]) instead of just ";". Also, it seems to prevent the semi-colons from being removed completely in the event that a user clicks the button twice. This wont screw up the functionality of your expression in some way that I have not tested, will it?
onclick="document.getElementById('<%=TextBox1.ClientID%>').value = (document.getElementById('<%=TextBox1.ClientID%>').value.replace(/[\(\)\-\,\.\;]+/g,'').replace(/\b(\d{3})\s*(\d{3})\s*(\d{4})\b\s*/g,'$1$2$3; ').replace(/(^\s*|[\;\s]*$)/g,''));"

Open in new window

Hey, you have to make it work, I am only the helper.  Is it workin?
I had already removed ; and added them again. So it has not to be a problem. They are always added as separators. I see you added also a space after semicolon for list separator. That is ok.

Great thanks again for your time
Works perfectly. Thanks!
You are welcome.