JS Special character filter sample code _javascript tips

Source: Internet
Author: User

Copy Code code as follows:

Match Chinese digit letter underline
var checkinput = function (str) {
var pattern =var pattern =/^[\w\u4e00-\u9fa5]+$/gi;
if (Pattern.test (c))
{
return false;
}
return true;
}

1.js filter special characters with regular expressions to verify that all input fields contain special symbols
Copy Code code 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 symbols
Copy Code code as follows:

/**
* Verify that all input fields contain special symbols
* The symbols to be filtered are written to the regular expression, noting that some symbols are to be escaped with ' \ '.
* Test Example:
* IF (Checkalltextvalid (Document.forms[0]))
* Alert ("All text boxes in the form pass the checksum!") ");
*/
function Checkalltextvalid (form) {
Record the number of text boxes that do not contain quotes
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;
Here to fill in the special symbols to be filtered
Note: Modify the ### #处的字符, other parts are not allowed to modify.
if (/^[^####]*$/.test (Form.elements[i].value))

if (/^[^\| "' <>]*$/.test (Form.elements[i].value))
Resulttag = 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
*/
if (Resulttag = = flag)
return true;
else {
Alert ("text box cannot contain \ n \ nthe 1 single quotes: ' \ n 2 double quotes: \" \ n 3 vertical bars: | \ n 4 SHARP horn: < > \ n) 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.