Sample Code for js special character filtering

Source: Internet
Author: User

Sample Code for js special character filtering

This article mainly introduces the sample code for js special character filtering. For more information, see.

The Code is as follows:

// Match Chinese characters, numbers, and underscores

Var checkInput = function (str ){

Var pattern =/^ [wu4e00-u9fa5] + $/gi;

If (pattern. test (c ))

{

Return false;

}

Return true;

}

 

1. Use Regular Expressions in js to filter out special characters and check whether all input fields contain special characters.

The Code is as follows:

Function stripscript (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;

}

 

2. Verify that all input fields contain special characters

The Code is as follows:

/**

* Check whether all input fields contain special characters

* The symbol to be filtered is written into the regular expression. Note that some symbols must be escaped using.

* Example:

* If (checkAllTextValid (document. forms [0])

* Alert ("all text boxes in the form are verified! ");

*/

Function checkAllTextValid (form ){

// Record the number of text boxes without quotation marks

Var resultTag = 0;

// Record the number of all text boxes

Var flag = 0;

For (var I = 0; I <form. elements. length; I ++ ){

If (form. elements [I]. type = "text "){

Flag = flag + 1;

// Enter the special characters to be filtered.

// Note: Modify the #### character. do not modify the other part.

// If (/^ [^ ####] * $/. test (form. elements [I]. value ))

 

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

ResultTag = resultTag + 1;

Else

Form. elements [I]. select ();

}

}

 

/**

* If the enclosed text box is equal to the value of all text boxes, the verification is successful.

*/

If (resultTag = flag)

Return true;

Else {

Alert ("the text box cannot contain nn 1 single quotes: 'n' 2 double quotes:" n 3 vertical bars: | n 4 tip: <> nn check 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.