Solved

Filtering strings in multiline textbox

Posted on 2008-10-02
13
671 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Author Comment

by:austerhaus
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Great thanks again for your time
0
 
LVL 1

Author Closing Comment

by:austerhaus
Comment Utility
Works perfectly. Thanks!
0
 
LVL 63

Expert Comment

by:Zvonko
Comment Utility
You are welcome.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
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)

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now