Home > Web Front-end > JS Tutorial > body text

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

WBOY
Release: 2016-05-16 15:05:57
Original
2998 people have browsed it

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,"");
}
Copy after login

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;
}
Copy after login

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

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

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!