?
Solved

Filtering strings in multiline textbox

Posted on 2008-10-02
13
Medium Priority
?
710 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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 code hide show and toggles. 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…
Suggested Courses

752 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