This is a migrated thread and some comments may be shown as answers.

[Solved] Block special characaters in filtercolumn in grid.

5 Answers 278 Views
Grid
This is a migrated thread and some comments may be shown as answers.
unnikkannan m
Top achievements
Rank 1
unnikkannan m asked on 18 Jan 2010, 05:44 AM
Hai Telerik Team,

 I'm using radgrid to show the details and has given the filtering property of the rad grid. But when entering the special charcters for filtering it causes the error. I tried to get the filter string on the client side and checked for special characters, if found then to cancel the filter operation. But if i gave '|' along with the filter contents it also caused error.
If its possible to block the special from entering the filter box it would be better. Please give me a solution.

Regards & Thanks
Unni.

5 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 18 Jan 2010, 07:36 AM
Hello Unnikkannan,

Try attaching 'onkeyup' event for filter textboxes in order to prevent the user from typing special characters.

CS:
 
    protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) 
    { 
        if (e.Item is GridFilteringItem) 
        { 
            GridFilteringItem fItem = (GridFilteringItem)e.Item; 
            foreach (GridColumn col in RadGrid1.MasterTableView.Columns) 
            { 
                (fItem[col.UniqueName].Controls[0] as TextBox).Attributes.Add("onkeyup""CheckSpecial(this, event)"); 
            } 
        }  
    } 

JavaScript:
 
    function CheckSpecial(text, e) { 
        var regx, flg; 
        regx = /[^0-9a-zA-Z'' ]/ 
        flg = regx.test(text.value); 
        if (flg) { 
            var val = text.value; 
            val = val.substr(0, (val.length) - 1) 
            text.value = val; 
        } 
    }  

-Shinu.
0
unnikkannan m
Top achievements
Rank 1
answered on 18 Jan 2010, 11:52 AM
Hai Shinu,

  Thanks for the quick reply and it worked. One more doubt can we avoid pasting the special characters by firing the onvaluechanged event() of the filtering texxtbox. which event is needed to be fired for avoid pasting special characters.

Regards
Unni

0
Accepted
Princy
Top achievements
Rank 2
answered on 19 Jan 2010, 11:53 AM
Hi Unni,

Try out the following code to avoid pasting special characters into the textbox:
c#:
  protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e)  
    {  
        if (e.Item is GridFilteringItem)  
        {  
            GridFilteringItem fItem = (GridFilteringItem)e.Item;  
            foreach (GridColumn col in RadGrid1.MasterTableView.Columns)  
            {  
                (fItem[col.UniqueName].Controls[0] as TextBox).Attributes.Add("onPaste""Paste_Event();");  
            }  
        }   
    }  

js:
function Paste_Event()  
   {            
            var objTxtBox = window.event.srcElement; 
            var PasteData = window.clipboardData.getData("Text"); 
            var regx, flg; 
            regx = /[^0-9a-zA-Z'' ]/ 
            flg = regx.test(PasteData); 
            if (flg) { 
                var val = PasteData; 
                event.returnValue = false
            }  
   } 

Hope this helps..
Princy.
0
Maria Isabel Henao
Top achievements
Rank 1
answered on 19 Feb 2010, 01:51 PM
Hi,

I tried the Paste_Event function and it works just perfect on IE. Is there a way to make it work on Firefox? I understand the issue is getting access to the clipboard content on Firefox, do you know any workaround to effectively block special characters on a paste event?

thanks ahead,

Maria Henao
0
Maria Isabel Henao
Top achievements
Rank 1
answered on 19 Feb 2010, 03:46 PM
Nevermind! I solved it by using onBlur instead of OnPaste event and checking/formatting the filter value there.

function CheckFilterValue(controlId) 
{     
    var filterControl = $get(controlId);     
    filterControl.value = filterControl.value.replace(regExp"");     
    return true

Tags
Grid
Asked by
unnikkannan m
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
unnikkannan m
Top achievements
Rank 1
Princy
Top achievements
Rank 2
Maria Isabel Henao
Top achievements
Rank 1
Share this question
or