Solved

strip bad characters from a textarea

Posted on 2004-03-28
12
477 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Suggested Solutions

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…
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

777 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