Home >Web Front-end >JS Tutorial >How to prevent SQL injection in JS code (super simple)_javascript skills

How to prevent SQL injection in JS code (super simple)_javascript skills

WBOY
WBOYOriginal
2016-05-16 15:05:573086browse

The following will introduce to you how to prevent SQL injection in js code from two aspects. It is very simple and practical. Friends who are interested can refer to it!

1. URL address injection prevention:

//过滤URL非法SQL字符
var sUrl=location.search.toLowerCase();
var sQuery=sUrl.substring(sUrl.indexOf("=")+1);
re=/select|update|delete|truncate|join|union|exec|insert|drop|count|'|"|;|>|<|%/i;
if(re.test(sQuery))
{
alert("请勿输入非法字符");
location.href=sUrl.replace(sQuery,"");
}

2. Input text box anti-injection:

/Prevent SQL injection

function AntiSqlValid(oField )
{
re= /select|update|delete|exec|count|'|"|=|;|>|<|%/i;
if ( re.test(oField.value) )
{
//alert("请您不要在参数中输入特殊字符和SQL关键字!"); //注意中文乱码
oField.value = ";
oField.className="errInfo";
oField.focus();
return false;
}

Add the following method to the input text box that needs to be protected from injection

txtName.Attributes.Add("onblur", "AntiSqlValid(this)");//防止Sql脚本注入 

The editor will tell you so much about how to prevent SQL injection in JS code. I hope it will be helpful to you!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn