View Single Post
  #1 (permalink)  
Old February 24th, 2006, 05:26 AM
crmpicco crmpicco is offline
Friend of Wrox
Points: 8,019, Level: 38
Points: 8,019, Level: 38 Points: 8,019, Level: 38 Points: 8,019, Level: 38
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2005
Location: Mauchline, East Ayrshire, Scotland
Posts: 1,525
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to crmpicco Send a message via AIM to crmpicco Send a message via MSN to crmpicco Send a message via Yahoo to crmpicco
Default javascript remove bad characters for MySQL db hit

Code:
function fix_chars(id,val)
{
    if ((typeof(val)=="undefined")||(typeof(id)=="undefined")){
        return;
    }
    if(/'/.test(val))
    {
        document.forms["fexp"].elements[id].value = val.replace(/'/g,'');
    }
    if(/&/.test(val))
    {
        document.forms["fexp"].elements[id].value = val.replace(/&/g,'');
    }
    if(/_/.test(val))
    {
        document.forms["fexp"].elements[id].value = val.replace(/_/g,'');
    }
    if(/,/.test(val))
    {
        document.forms["fexp"].elements[id].value = val.replace(/,/g,'');
    }
    if(/%/.test(val))
    {
        document.forms["fexp"].elements[id].value = val.replace(/%/g,'');
    }
    if(/`/.test(val))
    {
        document.forms["fexp"].elements[id].value = val.replace(/`/g,'');
    }
    if(/"/.test(val))
    {
        document.forms["fexp"].elements[id].value = val.replace(/"/g,'');
    }
    if(/@/.test(val))
    {
        document.forms["fexp"].elements[id].value = val.replace(/@/g,'');
    }
    if(/~/.test(val))
    {
        document.forms["fexp"].elements[id].value = val.replace(/~/g,'');
    }
    if(/#/.test(val))
    {
        document.forms["fexp"].elements[id].value = val.replace(/#/g,'');
    }
}

<input type="text" name="depapt" id="depapt" onKeyUp="fix_chars('depapt',this.value);" />
Is there anyway to cut this function 'fix_chars' down? Am i missing something?
My object is basically to remove any bad characters to stop it crashing my MySQL Database.

Picco

www.crmpicco.co.uk
__________________
_______________________
Ayrshire Minis - a Mini E-Community
http://www.ayrshireminis.com
http://www.crmpicco.co.uk
Reply With Quote