Solved

strip bad characters from a textarea

Posted on 2004-03-28
12
479 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone 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

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

791 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