Example code that deletes special characters and limits the length of user input

Source: Internet
Author: User

Copy codeThe Code is as follows:
/Check invalid characters and length
Function checkSpeChar (obj, byteLength, title ){
Var value = document. getElementById (obj). value;
Value = value. replace (/(^ \ s *)/g, ""). replace (/(\ s * $)/g ,"");
Var ret = (/[-|; |, | \/| \ (| \) | \ [| \] | \} | \ {| % | @ | \ * |! | \ ']/. Test (value ));
Var arr = ["@", "#", "$", "%", "^", "&", "*", "<", "> ", "/","\\","\'","'",". ",":","? "," [","] ","; ","-"," (",") "," _ "," \ "", "~ "," Insert "," delete "," update "," select "];
Value = value. toLowerCase ();
For (var I = 0; I <arr. length; I ++ ){
If (value. indexOf (arr [I]. toLowerCase ()> = 0 ){
Ret = true;
}
}
If (ret = true ){
Top. LSAlert ("the content contains invalid characters. Please try again! ");
Document. getElementById (obj). value = '';
Return;
}
LimitLength (value, byteLength, title, obj );
}
// Verify the length of input characters
Function limitLength (value, byteLength, title, attribute ){
Value = value. replace (/\ r/g, ""). replace (/\ n/g, ""); // press enter to replace two spaces
Var newvalue = value. replace (/[^ \ x00-\ xff]/g ,"***");
Var length = newvalue. length;
// When the number of bytes entered is smaller than the set number of bytes
If (length * 1 <= byteLength * 1 ){
Return;
}
Var limitDate = newvalue. substr (0, byteLength );
Var count = 0;
Var limitvalue = "";
For (var I = 0; I <limitDate. length; I ++ ){
Var flat = limitDate. substr (I, 1 );
If (flat = "*"){
Count ++;
}
}
Var size = 0;
Var istar = newvalue. substr (byteLength * 1-1, 1); // check whether the checkpoint is "×"
// If the base point is;
If (count % 3 = 0 ){
Size = count/3 + (byteLength * 1-count );
Limitvalue = value. substr (0, size );
}
Else if (count % 3 = 1 ){
Size = (count-1)/3 + (byteLength * 1-count );
Limitvalue = value. substr (0, size );
}
Else if (count % 3 = 2 ){
Size = (count-2)/3 + (byteLength * 1-count );
Limitvalue = value. substr (0, size );
}
// Top. LSAlert (title + "maximum input" + byteLength + "bytes! ");
Document. getElementById (attribute). value = limitvalue;
Return;
}

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.