How to write code that deletes special characters and limits the length of user input
This article introduces the knowledge of "how to write code that deletes special characters and limits the length of user input". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!
The copy code is as follows:
/ check for illegal characters and check character 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 ("illegal characters in the content, please re-enter it!")
Document.getElementById (obj). Value ='
Return
}
LimitLength (value, byteLength, title, obj)
}
/ / verify the input character length
Function limitLength (value, byteLength, title, attribute) {
Value = value.replace (/\ rCMG, ") .replace (/\ nUniqg,"); / / enter line feed to replace two spaces respectively
Var newvalue = value.replace (/ [^\ X00 -\ xff] / g, "* *")
Var length = newvalue.length
/ / when the number of bytes filled in is less than the set number of bytes
If (length * 1