JS Special character Filter Sample code

Source: Internet
Author: User
Tags regular expression

  This article is mainly on the JS special character filter Sample code is introduced, the need for friends can come to the reference, hope to help everyone else

  Code as follows://Match Chinese digit letter underscore         var checkinput = function (str) {      &NBS P     var pattern =var pattern =/^[wu4e00-u9fa5]+$/gi;             if (Pattern.test (c))             {    &N Bsp           return false;                         return true;        }   1.js filter special characters with regular expressions to verify that all input fields contain special symbols The   code is as follows: function Stripscript (s) {  &nbs P var pattern = new RegExp ("[' ~!@#$^&* () =|{} ':; '", [].<>/?~! @#¥......&* ()--| {}【】‘;:”“'。 ,、? ]         var rs = "";     for (var i = 0; i < s.length i++) {        rs = rs + s.substr (I, 1). replace (pattern , '');    }     return RS;   2. Verify that all input fields contain special symbols   code as follows:/**  * Verify that all input fields contain special symbols  * the symbols to be filtered are written to the regular expression, note that some symbols are used' Escaped.  * 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 all text box quantity     var flag = 0;     for (var i = 0; i < form.elements.length i++) {        if (Form.elements[i].type = = "Text" {            flag = flag + 1;            //fill in the filter Special symbols            /Note: Modify ### #处的字符, other parts are not allowed to modify.            //if (/^[^####]*$/.test (form.elements[i].value))         &NB Sp     if (/^[^| "' <>]*$/.test (Form.elements[i].value))                 Resulttag = Resulttag + 1;             else                 form.elements[i].se Lect ();         {   }      /**      * If a text box with quotes is equal to the value of all text boxes, the checksum passes the     &NB sp;*/    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;    }}

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.