?
Solved

Replace non-alphanumeric characters by underscore.

Posted on 2001-06-28
13
Medium Priority
?
3,077 Views
Last Modified: 2007-11-27
My goal is to replace all non-alphanumeric characters by '_'.  The following is the code I had.  At the end, when I display the final string, it was empty.  Please help me.  Thanks in advance.

==================================================
var str = "a b.3-4+";     // final string should be "a_b_3_4_"
var choice = "";

for ( i = 0; i < str.length; i++ )
    if ( str.charAt( i ) == ' ' || str.charAt( i ) == '-' ||
            str.charAt( i ) == '+' || str.charAt( i ) == '=' ||
            str.charAt( i ) == '*' || str.charAt( i ) == '&' ||
            str.charAt( i ) == '%' || str.charAt( i ) == '$' ||
            str.charAt( i ) == '@' || str.charAt( i ) == '#' ||
            str.charAt( i ) == ',' || str.charAt( i ) == '.' ||
            str.charAt( i ) == '>' || str.charAt( i ) == '<' )
        choice.concat( "_" );
    else
        choice.concat( str.charAt( i ));

alert( choice );
=========================================

Is there a better way to achieve what I wanted?

Thanks experts.
0
Comment
Question by:szetoa
[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
  • 6
  • 3
  • 2
  • +2
13 Comments
 
LVL 7

Expert Comment

by:daniel_c
ID: 6235845
listening...
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6235886
First of all here is a much more efficient solution:

<html>
<head>
<script language="javascript">
<!--

function convertName(val, resultObj){
  var test = new RegExp("[^0-9,a-z,A-Z]", "g");

  resultObj.value = val.replace(test, "_");
}

//-->
</script>
</head>
<body>
<form>
<input type="text" name="str1" value="">
<p><input type="button" value="convert..." onClick="convertName(this.form.str1.value, this.form.str2);"></p>
<input type="text" name="str2" value="">
</form>
</body>
</html>

Ant
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6235891
And here is the working version of your code:

<html>
<head>
<script language="javascript">
<!--

var str = "a b.3-4+";     // final string should be "a_b_3_4_"
var choice = "";

for ( i = 0; i < str.length; i++ )
   if ( str.charAt( i ) == ' ' || str.charAt( i ) == '-' ||
           str.charAt( i ) == '+' || str.charAt( i ) == '=' ||
           str.charAt( i ) == '*' || str.charAt( i ) == '&' ||
           str.charAt( i ) == '%' || str.charAt( i ) == '$' ||
           str.charAt( i ) == '@' || str.charAt( i ) == '#' ||
           str.charAt( i ) == ',' || str.charAt( i ) == '.' ||
           str.charAt( i ) == '>' || str.charAt( i ) == '<' )
       choice += "_";
   else
       choice += str.charAt(i);

alert( choice );

//-->
</script>
</head>
<body>
</body>
</html>
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
LVL 12

Accepted Solution

by:
ahosang earned 0 total points
ID: 6235904
use regular expressions:
<html>
<head>
     <title>Untitled</title>
<script>
function replaceChars(textField) {
  var nonAlphaNum=/[^0-9,a-z,A-Z]/gi;
  textField.value=textField.value.replace(nonAlphaNum, "_");
}
</script>
</head>

<body>

test<br>
<form>
<input name="text1" type="text" value="">
<input type="button" value="check it" onclick="replaceChars(this.form.text1)">
</form>

</body>
</html>
0
 
LVL 12

Expert Comment

by:ahosang
ID: 6235923
Dang Ant, you're pretty sharp mister :-)
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6235936
;o)

Ant
0
 
LVL 1

Author Comment

by:szetoa
ID: 6235948
Thank you both Ant and ahosang.  The regular expression solution works like a charm.  Since Ant gave me the answer first, I am going to award the points to him.  Thanks again.
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6235955
Think you made a bit of a mistake there then szetoa......the button is at the top of the comment that you want to accept......

Be careful in the future.... :o)

I shall get Community Support to resolve this.

:o)

Ant
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6235960
0
 
LVL 1

Author Comment

by:szetoa
ID: 6235978
Thank you both Ant and ahosang.  The regular expression solution works like a charm.  Since Ant gave me the answer first, I am going to award the points to him.  Thanks again.
0
 
LVL 1

Author Comment

by:szetoa
ID: 6235992
Sorry Ant,  I am a little confused about the button. I just posted another question with the same amount of points just for you.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 6236601
Points refunded so that they can be used in the question posted by szetoa for A.marsh.

Thank you
Computer101
Community Support Moderator
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6236930
Just to try to keep things all tied together, points awarded here:

http://www.experts-exchange.com/jsp/qShow.jsp?qid=20143064

:o)

Ant
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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)
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

741 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