Solved

Filtering strings in multiline textbox

Posted on 2008-10-02
13
693 Views
Last Modified: 2010-04-21
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
Comment
Question by:austerhaus
  • 8
  • 5
13 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 22625990
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 22626031
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 22626048
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:austerhaus
ID: 22626783
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 22627415
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
 
LVL 1

Author Comment

by:austerhaus
ID: 22628207
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 22628809
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
 
LVL 63

Accepted Solution

by:
Zvonko earned 500 total points
ID: 22628837
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
 
LVL 1

Author Comment

by:austerhaus
ID: 22628993
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
 
LVL 63

Expert Comment

by:Zvonko
ID: 22629036
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
 
LVL 1

Author Comment

by:austerhaus
ID: 22629053
Great thanks again for your time
0
 
LVL 1

Author Closing Comment

by:austerhaus
ID: 31502439
Works perfectly. Thanks!
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 22632048
You are welcome.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question