?
Solved

strip bad characters from a textarea

Posted on 2004-03-28
12
Medium Priority
?
485 Views
Last Modified: 2010-05-18
i am looking for a script that will strip all problematic characters from a textarea (except letters, numbers, commas & fullstops)

any assistance in this area will be appreciated.

thanks
0
Comment
Question by:hamishd
[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
12 Comments
 
LVL 4

Accepted Solution

by:
g_damodar earned 400 total points
ID: 10701553
<script>  
   
   function callForm()
   {
      errorflag=0
      s=document.f1.t1.value
      if(s.length>0)
      {
            err=stringCheck(document.f1.t1.value,"Text Area")
            if(err.length>0)
            {
                  alert(err);
                  errorflag=1
            }
      }
      if(errorflag==0)
      {
            document.f1.submit();
      }

   }
   function stringCheck(element,var2)
   {
         var err1 ='';
         val1 = element.charAt(0);
       if(!binSearch(val1))
       {
             err1 = err1.concat(var2.concat(":"));
             err1 = err1.concat("First Character Should be Alphabet");
             return err1
      }
            element = element.toUpperCase();
            count=0;
            for(i=0; i<element.length; i++)
            {
               var1 = element.substring(i,i+1);
           if(!binSearch(var1) && var1!="," && var1!="."&&!(var1>=0 && var1<=9) )
               {
                        err1 = err1.concat(var2.concat(":"));
                              err1 = err1.concat("Should contain only  alphabets, numbers , commas (,) and fullstops(.) ");
                  break;
                  }
            }
                      return err1;

   }

    function binSearch(chr)
    {
      alph = new Array("A","B","C","D","E","F","G","H","I","J","K","L","M",
       "N","O","P","Q","R","S","T","U","V","W","X","Y","Z",
        "a","b","c","d","e","f","g","h","i","j","k","l","m",      
        "n","o","p","q","r","s","t","u","v","w","x","y","z");
     
           lb = 0;
           ub = (alph.length+1);
         mid = Math.round((lb+ub)/2);  
         while((lb<=ub) && (chr!=alph[mid]))
           {
            if(chr < alph[mid])
            {
                   ub = mid-1;
             }
            else
            {
                     lb = mid+1;
              }
      
            mid = Math.round((lb+ub)/2);  
      }
        if (chr==alph[mid])
            return true
           else
            return false
    }
</script>

<form name=f1>
<textarea name=t1>
</textarea>
<input type=button onClick="javascript:callForm()">
0
 
LVL 23

Assisted Solution

by:Saqib Khan
Saqib Khan earned 400 total points
ID: 10701618
<html>
<head>
<title>dd</title>
<Script>
function strip(txt)      {
var reg = new RegExp(/^[\d|a-z|A-Z|,.]+$/)

if(reg.test(txt)){      
alert("Pass")
return true
}
else      {
alert("Fail")
return false;
}

      }

</Script>
</head>
<body>
<form name="f1" onSubmit="return strip(t1.value)">
<input type="text" name="t1">
</form>
</body>
</html>
0
 
LVL 63

Assisted Solution

by:Zvonko
Zvonko earned 400 total points
ID: 10701692
Upper regexp will forbid blanks but allow pipe char.

Check this version:

<html>
<head>
<script>
function validate(theArea){
  var badChr = /[^a-z0-9\,\.\!\?\ ]/gi;
  if(theArea.value.match(badChr)){
    theArea.value=theArea.value.replace(badChr,'');
  }
}
</script>
</head>
<body>
<form>
<textarea name="UserInput" rows="10" cols="80" onKeyUp="validate(this)">
</textarea>
</form>
</body>
</html>

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 63

Expert Comment

by:Zvonko
ID: 10702098
Short description: the badChar are all characters which are NOT listed in the square brackets.
The not char is the first ^ in the set of chars in the brackets.
0
 
LVL 31

Assisted Solution

by:GwynforWeb
GwynforWeb earned 400 total points
ID: 10702315
Here is my try, it allows through linefeeds and tabs (unlike the above)

as an inline function
<textarea onkeyup="this.value=this.value.replace(/[^\w,\.\s ]/gi,'')">

</textarea>

as a full function

<script>
function rep(val){
return val.replace(/[^\w\,\!\.\'\;\s ]/gi,'')
}
</script>

<textarea onkeyup="this.value=rep(this.value)">

</textarea>
0
 
LVL 25

Assisted Solution

by:devic
devic earned 400 total points
ID: 10704015
here an example where user wihtout regexp knowledges, can easy manipulate with bad characters :
===============
<textarea onkeyup="this.value=this.value.replace(/[^\w,\.\s ]/gi,'')">
</textarea>
<script>
function rep(val)
{
      var badCharacters =["]",";","+"]
      for(var i=0;i<badCharacters.length;i++)
      val.replace(eval("/"+badCharacters[i]+"/gi"),'');
      return val;
}
</script>
0
 
LVL 25

Expert Comment

by:devic
ID: 10704191
oops, shame to me, copy paste error :)
===========
<textarea onkeyup="rep(this)">
</textarea>
<script>
function rep(val)
{
      var badCharacters =["]",";","+"]
      for(var i=0;i<badCharacters.length-1;i++)
      val.value=val.value.replace(eval("/"+badCharacters[i]+"/gi"),"");
}
</script>
0
 
LVL 6

Expert Comment

by:ren_b
ID: 10704472
devic, the only bad thing about your's is there are a LOT more "bad" characters than good ones, ie different ranges in unicode.

using a regex like the ones contributed by gwynforweb or zvonko, imo, is the way to go.
0
 
LVL 25

Expert Comment

by:devic
ID: 10704569
ren_b  your are right,
but if i want ":" but not ";" etc. and i can simple decide bad chars.
then i think my example is better
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
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

801 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