Two simple JS codes to prevent SQL Injection

Source: Internet
Author: User

1. url address anti-injection:

// Filter invalid SQL characters in the URL
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 ("do not enter invalid characters ");
Location. href = Surl. Replace (squery ,"");
}

 

2. Enter the text box to prevent injection:

Introduce the following JS

1 // prevents SQL Injection
2 function antisqlvalid (ofield)
3 {
4 Re =/select | update | Delete | exec | count | '| "| = | ;|>|||%/ I;
5 If (Re. Test (ofield. Value ))
6 {
7 // alert ("Please do not enter special characters and SQL keywords in parameters! "); // Note Chinese garbled characters
8 ofield. value = ";
9 ofield. classname = "errinfo ";
10 ofield. Focus ();
11 return false;
12}

Add the following method to the input text box that requires anti-injection:

1 txtname. Attributes. Add ("onblur", "antisqlvalid (this)"); // prevents SQL Script Injection

Source: http://www.51obj.cn/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.