Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 720
  • Last Modified:

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
0
austerhaus
Asked:
austerhaus
  • 8
  • 5
1 Solution
 
ZvonkoSystems architectCommented:
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

0
 
ZvonkoSystems architectCommented:
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

0
 
ZvonkoSystems architectCommented:
The extra \b checking was necessary to prevent digits borrow to neigbour when too many or less digits were in one telefon number group.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
austerhausAuthor Commented:
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.
0
 
ZvonkoSystems architectCommented:
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

0
 
austerhausAuthor Commented:
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

0
 
ZvonkoSystems architectCommented:
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

0
 
ZvonkoSystems architectCommented:
Sorry, missed some quote chars:
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

0
 
austerhausAuthor Commented:
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

0
 
ZvonkoSystems architectCommented:
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.

0
 
austerhausAuthor Commented:
Great thanks again for your time
0
 
austerhausAuthor Commented:
Works perfectly. Thanks!
0
 
ZvonkoSystems architectCommented:
You are welcome.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now