JS Filter Special character function

Source: Internet
Author: User
Keywords Web page production Ajax javascript
Tags ajax function functions javascript return site text text box

JS Filter special character function to filter some illegal, there may be injected to the site browsing impact of special strings or characters Oh, we have two of these functions.

function Strips Tutorial Cript (s)


{


var pattern = new RegExp ("[' ~!@#$^&* () =|{} ':; '", [].<>/?~! @#¥......&* ()--| {}【】‘;:”“'。 ,、? ]")


var rs = "";


for (var i = 0; i < s.length; i++) {


rs = Rs+s.substr (i, 1). replace (pattern, ');


}


return RS;


}

Method Two

* Verify that all input fields contain special symbols


* The symbols to be filtered are written to the regular expression, noting that some symbols are "escaped".


* The characters to be escaped include: 1, Dot.


* 2, bracket []


* 3, curly brace {}


* 4, plus +


* 5, asterisk *


* 6, minus-


* 7, Slash


* 8, vertical bar |


* 9, Apex ^


* 10, Coins $


* 11, question mark?


* Sample:


* if (Checkalltextvalid (document.forms[0))


* Alert ("All text boxes in the form pass the checksum!")     ");


 */

function Checkalltextvalid (form)


{     


//record number of text boxes without quotes


var resulttag = 0;


//record number of all text boxes


var flag = 0;


for (var i = 0; i < form.elements.length i + +)


 {     


if (form.elements[i].type== "text")


  {     


Flagflag = flag + 1;


//Here fill in the special symbols to be filtered


Note: Change the characters at #, and other parts are not allowed to be modified.


//if (/^[^#]*$/.test (form.elements[i].value))


    


if (/^[^| "' <>]*$/.test (form.elements[i].value))


Resulttagresulttag = resulttag+1;


Else


Form.elements[i].select ();


  }    


 }    


   


    /**    


* If a text box with quotes is equal to the value of all text boxes, the checksum passes through the


     */


if (Resulttag = = flag)


return true;


Else


 {    


alert ("text box cannot contain nn 1 single quotes: ' N 2 double quotes:" N 3 Vertical bars: | N 4 Sharp Corner number: < > NN please check the input!      ");


return false;


 }     


}


Related Article

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.