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;5555 555555
Your assistance is greatly appreciated. Please let me know if additional detail is required.
Thanks
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;5555
Your assistance is greatly appreciated. Please let me know if additional detail is required.
Thanks
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>
The extra \b checking was necessary to prevent digits borrow to neigbour when too many or less digits were in one telefon number group.
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>
ASKER
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?
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*$/,''));"
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,'));"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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,''));"
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.
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.
ASKER
Great thanks again for your time
ASKER
Works perfectly. Thanks!
You are welcome.
But this is my concept:
Open in new window