Solved

strip bad characters from a textarea

Posted on 2004-03-28
12
481 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
12 Comments
 
LVL 4

Accepted Solution

by:
g_damodar earned 100 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 100 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 100 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
Technology Partners: 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!

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Understanding UserAgent String 2 48
href return to normal window 5 31
javascript form submition 2 22
How do I add counter to html listbox using jquery 2 26
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-…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

733 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